Robotics & Artificial Intelligence
Write a user-defined function def Sum_Digits() to input a number from the user. The program calculates sum of the digits of the number inside the function without returning any data to the main program.
Sample Input: 345
Output: 3 + 4 + 5 = 12
Python Functions
1 Like
Answer
def Sum_Digits(n):
s = 0
temp = n
while(n != 0):
d = n % 10
s = s + d
n = n // 10
print("Sum of digits =", s)
# main program
num = int(input("Enter a number:"))
Sum_Digits(num)Output
Enter a number:345
Sum of digits = 12
Answered By
1 Like
Related Questions
Write a user-defined function
def Circle()to input radius of a circle from the user. The program calculates area and circumference inside the function. Finally, it returns the values to main program to display the result.Write a user-defined function
def Perfect()to input a number from the user. The program checks whether it is a perfect number or not. Finally, it returns the values to main program to display the result.A perfect number is a positive integer that is equal to the sum of its proper divisors, excluding itself.
For example, 6 = 1 + 2 + 3
It is a perfect number.
Write a user-defined function
def Rev_Digits()to input a number from the user. The program reverses the digits to produce a new number inside the function. However, it displays the new number inside the function without returning any data to the main program.Sample Input: 257
Output: 752
Write a user-defined function
def HCF()to input two numbers from the user. The program calculates greatest common divisor of the two numbers inside the function. Finally, it returns the value to main program to display the result.Sample Input: First Number: 25
Second Number: 60
HCF = 5