Program:1.
Write a program to take input for a number, calculate and display the sum of its digits.
def sum_of_digits(n): s=0 while n>0: d=n%10 s=s+d n=n//10 print("Sum of digits = ",s) def main(): a=int(input("Enter any no ")) sum_of_digits(a) #function calling main()
Output:
Enter any no 1234
Sum of digits = 10
Enter any no 1452
Sum of digits = 12
Program:2.
Write a program to take input for a number and calculate and display its factorial.
def fact(n): f=1 i=1 while i<=n: f=f*i i=i+1 print("Factorial = ",f) def main(): a=int(input("Enter any no ")) fact(a) """ if __name__=='__main__': main() """ #or #function calling main()
Output:
Enter any no 5
Factorial = 120
Enter any no 6
Factorial = 720
Program:3.
Write a Python program to pass a string to the function. Return and display longest word.
def longest_word(n): n1="" for i in n.split(): if len(i)>len(n1): n1=i print("longest word ",n1) def main(): #n=input("Enter any string ") n="Hello how are you greeting you" print("String is ",n) longest_word(n) """ if __name__=='__main__': main() """ #or #function calling main()
Output:
String is Hello how are you greeting you
longest word greeting