20 - Write a program in Python to input a number and determine the sum of the digits of that number.
n=int(input("Enter a number: "))
sum=0
while n>0:
r=n%10
n=n//10
sum=sum+r
print("Sum of digits=",sum)
n=int(input("Enter a number: "))
sum=0
while n>0:
r=n%10
n=n//10
sum=sum+r
print("Sum of digits=",sum)
a=0
b=1
n=int(input("Enter the number of terms to be printed for fibonacci Series: "))
print(a, b,end=' ')
for i in range(1,n-1):
c=a+b
print(c,end=' ')
a=b
b=c
Output:
Enter the number of terms to be printed for fibonacci Series: 10
0 1 1 2 3 5 8 13 21 34
1
234
56789
10111213141516
p=1
a=5
k=2
n=3
for i in range(1,5):
for s in range(1,a):
print(end=' ')
for j in range(p,k):
print(j,end='')
print()
a=a-1
p=j+1
k=k+n
n=n+2
Output:
1
234
56789
10111213141516
ABCDEFEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
y=71
sp=2
for i in range(1,8):
x=65
while(x<y):
print(chr(x),end='')
x=x+1
if i==1:
x=x-2
else:
x=x-1
for s in range(1,sp):
print(end=' ')
sp=sp+2
while(x>64):
print(chr(x),end='')
x=x-1
print()
y=y-1
Output:
ABCDEFEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
1
123
12345
1234567
123456789
sp=5
k=2
for i in range(1,6):
for s in range(1,sp):
print(end=' ')
for j in range(1,k):
print(j,end='')
print()
sp=sp-1
k=k+2
1
123
12345
1234567
123456789
*
**
***
****
*****
for i in range(1,6):
for j in range(1,i+1):
print("*",end='')
print()
*
**
***
****
*****
A
AB
ABC
ABCD
ABCDE
for i in range(1,6):
for j in range(1,i+1):
print(chr(j+64),end='')
print()
Output:
A
AB
ABC
ABCD
ABCDE
5
54
543
5432
54321
for i in range(5,0,-1):
for j in range(5,i-1,-1):
print(j,end='')
print()
Output:
5
54
543
5432
54321
54321
5432
543
54
5
for i in range(1,6):
for j in range(5,i-1,-1):
print(j,end='')
print()
Output:
54321
5432
543
54
5
55555
4444
333
22
1
for i in range(5,0,-1):
for j in range(1,i+1):
print(i,end='')
print()
Output:
55555
4444
333
22
1