Program statement
Write a program to calculate the sum of digits from number. Here we take a number as input from user and by using operator and loop we will calculate the sum of digit.
For Example
Let a number is 345,
then sum of digit is 17.
Program for sum of digits
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include<stdio.h> int main() { int num,sum=0,rem; printf("Enter a number:"); scanf("%d\n",&num); while(num>0) { rem=num%10; sum=sum+rem; num=num/10; } printf("Sum of digit is %d",sum); return 0; } |
Output:
1 2 |
Enter a number: 345 Sum of digit is 12 |