[go: up one dir, main page]

0% found this document useful (0 votes)
25 views1 page

Loops in Python

The document contains various Python programming examples demonstrating loops, arithmetic operations, and user input. It includes tasks such as printing numbers, calculating sums and products, generating multiplication tables, and finding factors and factorials of numbers. Additionally, it covers series generation like Fibonacci and squares, along with user-interactive prompts for input.

Uploaded by

sara3694836948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views1 page

Loops in Python

The document contains various Python programming examples demonstrating loops, arithmetic operations, and user input. It includes tasks such as printing numbers, calculating sums and products, generating multiplication tables, and finding factors and factorials of numbers. Additionally, it covers series generation like Fibonacci and squares, along with user-interactive prompts for input.

Uploaded by

sara3694836948
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

In [1]: for i in range(1,10):

print(i)

1
2
3
4
5
6
7
8
9

In [2]: # while loop


i=1
while i<=10:
print(i)
i=i+1

1
2
3
4
5
6
7
8
9
10

In [4]: #write a program to print first 10 even numbers


for i in range(2,21,2):
print(i,end=" ")

2 4 6 8 10 12 14 16 18 20

In [5]: # WAP to print the sum of first 10 natural numbers


sum=0
for i in range(1,11):
sum=sum+i
print("Sum of first 10 numbers=",sum)

Sum of first 10 numbers= 55

In [6]: # WAP to print the product of first 10 natural numbers


product=1
for i in range(1,11):
product=product*i
print("Product of first 10 numbers=",product)

Product of first 10 numbers= 3628800

In [8]: # What will be the output of following program?


for a in range(5,55,5):
print(a,end=" ")

5 10 15 20 25 30 35 40 45 50

In [11]: # WAP to accept a number and print its multiplication table


num=int(input("Enter a number="))
for i in range(1,11):
print(num*i,end=" ")

Enter a number=5
5 10 15 20 25 30 35 40 45 50

In [12]: # WAP to accept a number and print its multiplication table in proper format
num=int(input("Enter a number="))
for i in range(1,11):
print(num,"*",i,"=",num*i)

Enter a number=12
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
12 * 9 = 108
12 * 10 = 120

In [13]: # WAP to enter a number and print its factors


num=int(input("Enter a number="))
for i in range(1,num+1):
if num%i==0:
print(i,end=" ")

Enter a number=18
1 2 3 6 9 18

In [14]: # Write a program to enter a number and print its reverse


num=int(input("Enter a number="))
while num>0:
k=num%10
print(k,end="")
num=num//10

Enter a number=543
345

In [15]: # Write a program to enter a number and print sum of its digits
num=int(input("Enter a number="))
sum=0
while num>0:
k=num%10
sum=sum+k
num=num//10
print("Sum of digits=",sum)

Enter a number=345
Sum of digits= 12

In [17]: # WAP to enter 2 numbers and print all numbers from starting to end
a=int(input("Enter first number="))
b=int(input("Enter second number="))
for i in range(a,b+1):
print(i,end=" ")

Enter first number=6


Enter second number=10
6 7 8 9 10

In [18]: # WAP to print the fibonacci series 0 1 1 2 3 5 8 13 .....


a=0
b=1
print(a,b,end=" ")
c=0
while c<21:
c=a+b
print(c,end=" ")
a=b
b=c

0 1 1 2 3 5 8 13 21

In [19]: # WAP to enter a number and print its factorial


num=int(input("Enter a number="))
fact=1
for i in range(num,0,-1):
fact=fact*i
print("Factorial of number=",fact)

Enter a number=6
Factorial of number= 720

In [21]: # WAP to print the following series 1 4 9 16 25 .... n terms


n=int(input("How many terms?"))
for i in range(1,n+1):
print(i*i,end=" ")

How many terms?8


1 4 9 16 25 36 49 64

In [24]: # Find the sum of the following series 1 3 5 7 9 11 .... terms


n=int(input("How many terms?"))
a=1
sum=0
for i in range(1,n+1):
sum=sum+a
a=a+2
print("Sum=",sum)

How many terms?5


Sum= 25

In [25]: # What will be the output of the following code snippet?


i=1
y=3
while y<=10:
y=y+4
i=i+2
print(y)
print(i)

11
5

You might also like