5th Practical
5th Practical
X.
Output:
10
9
8
7
6
2. Change the following Python code from using a while loop to for loop.
x=1
while x<10:
print (x),
x+=1
Program:
x=1
for x in range(1,10):
print(x)
Output:
1
2
3
4
5
6
7
8
9
1.
a.
Program:
for i in range(1,5):
print("*"*i)
Output:
b.
Program:
rows=3
for i in range(1,rows+1):
print(" "*(rows-i)+"*"*(2*i-1))
for i in range(rows-1,0,-1):
print(" "*(rows-i)+"*"*(2*i-1))
Output:
c.
Program:
rows=4
for i in range(rows,0,-1):
print(" "*(rows-i)+"1010101"[:2*i-1])
Output:
2. WAP to print all even numbers from 1-100 using while loop.
Program:
i=1
while i<=100:
if(i%2==0):
print("",i,end=””)
i+=1
Output:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46
48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88
90 92 94 96 98 100
3. WAP to find out the sum of first 10 natural numbers using for loop.
Program:
sum=0
for i in range(1,10+1):
sum=sum+i
print(sum)
Output:
55
Output:
Output:
Output:
7. WAP which takes in a number and finds the sum of digits in a number.
Program:
number=int(input("Enter the number: "))
sum=0
while number>0:
n1=number%10
sum=sum+n1
number=number//10
print(sum)
Output:
Output: