Tuesday, 7 August 2018

11-C Program-10. Write a program to read a number and print the number of digits and the sum of the digits using function.


   Algorithm:
   Input
   1. n   # n=125
   Output:
   1. s  # s= 1+2+5=8  sum the digits
   Method
   def sum(n)  # called function
   1. s=0
   2.  while(n > 0)
         begin
           r=n%10
           n=n//10   # floor division operato
           s= s+r
         end
   3.  print s
 
   # main program 
   1. start
   2. read n
   3. write n
   4. sum(n)  # function calling
   5. stop
 

#program
#function
def sum(n):   #called function
    s=0
    while(n>0):
        r=n%10
        n=n//10
        s=s+r
    print("Sum of digits=",s)

#main program
n=int(input("Enter the number"))
print("The number is=",n)
sum(n)     #calling functin

Example:
Enter the number125
The number is= 125
Sum of digits= 8

   

No comments:

Post a Comment