M.Sc Python Programming Assignment 20

M.Sc Python Programming Assignments

Assignment 20

Question:

Write a Python program to get the sum of a non-negative integer.

Code:

				
					def sum_of_digits(n):
    if n < 10:
        return n
    else:
        return n % 10 + sum_of_digits(n // 10)

number = int(input("Enter a non-negative integer: "))

sum_result = sum_of_digits(number)

print("Sum of the digits:", sum_result)

				
			

Output:

				
					Enter a non-negative integer: 33
Sum of the digits: 6

				
			
Tech Amplifier Final Logo