In: Computer Science
Write a program that does the following:
You should test your program with at least the following test cases. (I am to be programming with python)
>>> RESTART: /Users/mamp/Desktop/Desktop - Enter a 5-digit number: 12345 15 >>> RESTART: /Users/mamp/Desktop/Desktop - Enter a 5-digit number: 00000 0 >>> RESTART: /Users/mamp/Desktop/Desktop - Enter a 5-digit number: 99999 45 >>> RESTART: /Users/mamp/Desktop/Desktop - Enter a 5-digit number: 20406 12 >>>
Please find your solution below and if any doubt comment and do upvote.
CODE:
#function to find the sum of 5 digit number
def sumOfDigit(num):
sum=0;#variable to store the sum of digits
#loop till number becomes 0
while(num!=0):
sum=sum+int(num%10) #take the last digit and to sum
num=int(num/10) #remove the last digit from the number
return sum
#take user input
num=int(input('Enter a 5-digit number: '))
#print the sum
print(sumOfDigit(num))
OUTPUT SCREENSHOT:
CODE SCREENTSHOT: