Algorithm
- Start
- Read a number ‘n’
- Assign m with n, sum with 0
- If n ≠ 0 repeat steps (5) thru (7) else go to (8)
- k ß modulus(n,10)
- sum ß sum + k
- n ß quotient of n/10
- Print sum of digits of n is sum
- Stop
Program
#include <stdio.h>
main()
{
int n,m,k,sum ;
clrscr() ;
printf("\tProgram to find Sum of Digits of a Number\n") ;
printf("\n Input\n\n") ;
printf("Enter a Number : ") ;
scanf("%d",&n) ;
m = n ;
sum = 0 ;
while (n != 0)
{
k = n%10 ;
sum += k ;
n = n/10 ;
}
printf("\n Output\n\n") ;
printf("Sum of Digits of %d is : %d",m,sum) ;
getch() ;
}
Output
Program to find Sum of Digits of a Number
Input
Enter a Number : 1729
Output
Sum of Digits of 1729 is : 19
Program to find Sum of Digits of a Number
Input
Enter a Number : -786
Output
Sum of Digits of -786 is : -21