Originally Posted by
Afik
I meant that the sum of digits of the number for example 1234, is 10, and the length of its digits is 4.
(...)
The functions work great.
Ah, how interesting. I compiled and ran this program:
Code:
#include <stdio.h>
int len(int n) {
int i, count = 1;
for (i = 0; i <= n; i++) {
count++;
n /= 10;
}
return count;
}
int sum(int a) {
int i, count = 0;
for (i = 0; i <= a; i++) {
count += (a % 10);
a /= 10;
}
return count;
}
int main(void) {
int n = 1234;
printf("Number of digits of %d is %d\n", n, len(n));
printf("Sum of digits of %d is %d\n", n, sum(n));
return 0;
}
The output that I received was:
Code:
Number of digits of 1234 is 4
Sum of digits of 1234 is 9
This contradicts your claim that the functions work great since the sum of digits is incorrect. Then I changed n to 0, and the output I received was:
Code:
Number of digits of 0 is 2
Sum of digits of 0 is 0
This contradicts your claim that the functions work great since the number of digits is incorrect.
EDIT:
Originally Posted by
Afik
The problem is probably in the int main....
Actually, I fixed len and sub and tested with your main function. While there's room for improvement, the revised program with your main function works great.