Tuesday, 13 February 2024

Class 11 CS Practical - 20

 

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)


Output:

Enter a number: 145
Sum of digits= 10

Enter a number: 346
Sum of digits= 13

No comments:

Post a Comment