0% found this document useful (0 votes)
36 views9 pages

Loops Python 241216 110517

Loops programs in Python.

Uploaded by

sumitkrr78
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)
36 views9 pages

Loops Python 241216 110517

Loops programs in Python.

Uploaded by

sumitkrr78
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/ 9

0k4rgvypz

December 2, 2024

[1]: # while loop


# wap to print counting
number=1
while(number<=10):
print(number)
number+=1

1
2
3
4
5
6
7
8
9
10

[7]: # wap to print table of any number


number=int(input("enter any number"))
count=1
while(count<=10):
result=number*count
print(result)
count+=1

enter any number 90


90
180
270
360
450
540
630
720
810
900

1
[15]: # wap to calculate even number from 1 to 30
number=1
while(number<=30):
if(number%2==0):
print(number)
number+=1

2
4
6
8
10
12
14
16
18
20
22
24
26
28
30

[17]: # wap to calculate odd number from 1 to 30


number=1
while(number<=30):
if(number%2!=0):
print(number)
number+=1

1
3
5
7
9
11
13
15
17
19
21
23
25
27
29

2
[23]: # wap to calculate factorial of any number----5*4*3*2*1-----1*5=5--1*4=4
number=int(input("enter any number"))
fact=1
while(number>0):
fact*=number
number-=1
print("the factorial of number is ",fact)

enter any number 4


the factorial of number is 24

[27]: # wap to swap two numner without using third number


a=int(input("enter the value of a "))
b=int(input("enter the value of b "))
a,b=b,a
print("swap value of a ",a)
print("swap value of b",b)

enter the value of a 58


enter the value of b 89
swap value of a 89
swap value of b 58

[7]: # wap to calculate factorial of a number


number=int(input("enter any number"))
fact=1
while(number>0):
fact*=number
number-=1
print("the factorial of number is ",fact)

enter any number 5


the factorial of number is 120

[17]: # wap to print fibonacci series upto 10


a=0
b=1
count=0
while(count<=10):
print(a)
a,b=b,a+b
count+=1

0
1
1

3
2
3
5
8
13
21
34
55

[11]: a=10
b=20
a,b=b,a
print(a)
print(b)

20
10

[3]: # wap to print pattern using while loop


# *
# **
# ***
# ****
# *****
num=5
i=1
while(i<=num):
print("*"*i)
i+=1

*
**
***
****
*****

[21]: # wap to print counting using for loop


for i in range(100,1,-2):
print(i)

100
98
96
94
92
90
88
86

4
84
82
80
78
76
74
72
70
68
66
64
62
60
58
56
54
52
50
48
46
44
42
40
38
36
34
32
30
28
26
24
22
20
18
16
14
12
10
8
6
4
2

[15]: # wap to print reverse of a string using for loop


string="ajit"
for char in reversed(string):
print(char)

5
t
i
j
a

[25]: # wap to calculate vowels present in the string


string=input("enter the string to count the vowels")
count=0
for char in string:
if char in "aeiouAEIOU":
print(char)
count+=1
print("number of vowels presnt in the string=",count)

enter the string to count the vowels hello


e
o
number of vowels presnt in the string= 2

[29]: # wap to print table of any number using for loop


number=int(input("enter any number"))
for i in range(1,11):
print(f"{number}*{i}={number*i}")

enter any number 5


5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

[33]: # wap to print factorial using for loop


number=int(input("enter any number"))
fact=1
for i in range(1,number+1):
fact*=i
print("factorial of a number is ",fact)

enter any number 5


factorial of a number is 120

6
[45]: # wap to print fibonacci series
a=0
b=1
n=int(input("enter the limit of your fibo series "))
for i in range(n):
print(a,end=" ")
a,b=b,a+b

enter the limit of your fibo series 10


0 1 1 2 3 5 8 13 21 34

[47]: # wap to print pattern using for loop


# *
# **
# ***
# ****
# *****
number=int(input("enter the number of rows you want to print"))
for i in range(1,number+1):
print("*"*i)

enter the number of rows you want to print 5


*
**
***
****
*****

[55]: number=5
for i in range(1,number+1):
for j in range(1,i+1):
print(i,end="")
print()

1
22
333
4444
55555

[63]: number=5
for i in range(number,0,-1):
for j in range(1,i+1):
print("*",end="")
print()

*****

7
****
***
**
*

[83]: # wap to print diamond pattern


row=int(input("enter the row for pyramid"))
for i in range(1, row+1):
print(" " * (row-i) + "*" * (2*i-1))
for i in range(row-1,0,-1):
print(" " * (row-i) + "*" * (2*i-1))

enter the row for pyramid 5


*
***
*****
*******
*********
*******
*****
***
*

[81]: # wap to print inverted pyramid


for i in range(row,0,-1):
print(" " * (row-i) + "*" * (2*i-1))

*********
*******
*****
***
*

[89]: # use of break


for i in range(1,10):
if i==5:
print(i)
break

[93]: # wap to make a calculator using while loop


while True:
number1=int(input("enter the value of number1"))
number2=int(input("enter the value of number 2"))
op=input("enter the operator in between +,-,*,/")
if (op=="+"):

8
result=number1+number2
print("the addition of number1 and number2=",result)
elif(op=="-"):
result=number1-number2
print("the subtraction of number1 and number2=",result)
elif(op=="*"):
result=number1*number2
print("the multiplication of number1 and number2=",result)
elif(op=="/"):
if (number2==0):
print("division not possible by zero please provide diff value")
else:
result=number1/number2
print("the division of number1 and number2=",result)
else:
print("please enter a valid operator ")
repeat=input("do you want to repeat press yes or No")
if repeat.lower()!="yes":
print("thank you for using my calculator")
break

enter the value of number1 23


enter the value of number 2 32
enter the operator in between +,-,*,/ +
the addition of number1 and number2= 55
do you want to repeat press yes or No yes
enter the value of number1 24
enter the value of number 2 23
enter the operator in between +,-,*,/ -
the subtraction of number1 and number2= 1
do you want to repeat press yes or No no

[97]: for i in range(10):


if i%2==0:
continue#skip the current iteration and print next
print(i)

1
3
5
7
9

[ ]:

You might also like