Calculate sum of digits of a number
Sample Output
Calculate sum of digits of a number. Number: 1345 Sum: 13
Source Code
#include<stdio.h>
int main()
{
int number, digit, sum = 0;
printf("Calculate sum of digits of a number.\n\nNumber: ");
scanf("%d", &number);
while(number != 0)
{
digit = (number % 10);
sum = sum + digit;
number = (number / 10);
}
printf("\nSum: %d\n\n", sum);
}
Sample Output
Calculate sum of digits of a number. Number: 897 Sum: 24
No comments:
Post a Comment