Find the number of digits and the digit sum of LLONG_MAX, the largest long long.
Solution
main.c
#include <limits.h>
#include <stdio.h>
int main(void) {
long long n = LLONG_MAX;
int digits = 0, sum = 0;
for (long long m = n; m > 0; m /= 10) {
sum += (int) (m % 10);
digits++;
}
printf("%lld has %d digits, digit sum %d\n", n, digits, sum);
return 0;
}
Output Verified by compiling and running
9223372036854775807 has 19 digits, digit sum 88
A 64-bit long long holds 19-digit numbers. The loop variable must be long long too – an int copy would truncate the value.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!