LearnFrenzy
Sign in Take a free test

Data Types · Data Type Programs · Question 3

Digit sum of a very large number (long long) in C Solution, output & explanation

  • Program
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 Back to Exercise

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.

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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links