Problem-Finding the sum of digits of five digit number.
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
// Complete the code to calculate the sum of the five digits on n.
int num;
int base = 10000;
int sum = 0;
// printf("%d\n",(num-(num%base))/base );
// printf("%d\n",(num%base));
for (int i = 1; i <= 5; i++)
{
num = (n - (n % base)) / base;
n = (n % base);
base /= 10;
sum += num;
}
printf("%d\n", sum);
return 0;
}
No comments:
Post a Comment