Problem: Program to sum of digits of a given number.
To sum of digits do the following steps:
- Get the remainder of a number N by modulo-division with 10 and sum the remainders.
- Remove last digit of a number N by dividing it with 10.
- Repeat steps 1 and 2 until the value of N becomes 0.
#include<stdio.h>
int main() {
int num, sum=0;
scanf("%d",&num);
while(num!=0){
sum = sum + num%10;
num /= 10;
}
printf("%d",sum);
return 0;
}
int main() {
int num, sum=0;
scanf("%d",&num);
while(num!=0){
sum = sum + num%10;
num /= 10;
}
printf("%d",sum);
return 0;
}
Sample Input
256
Sample Output
13
256
Sample Output
13