Sum of Digits of a Five Digit Number in C Full Code Solution

Learn how to calculate the sum of digits of a five digit number in C programming. Includes full code, explanation, beginner tips.

One of the most important skills in C programming is understanding how to work with numbers and manipulate digits. In this tutorial, TruongDevs presents a complete guide to solving a classic beginner exercise in C language. The problem is simple but essential. You are given a five digit integer, and you are required to calculate the sum of its digits. This problem will help you get comfortable with concepts like the modulo operator, integer division, loops, and basic I O operations using scanf and printf.

Many students learning C often encounter this type of exercise early in their programming journey. This is because understanding how to extract digits using the modulo operator and divide by 10 is a foundational logic technique used in many algorithm problems such as reversing a number, checking for palindromes, or digit-based validations. Mastering this problem is a great first step toward stronger problem solving skills in C.

C Program to Calculate Sum of Digits of a Five Digit Number
C Code to Sum Digits of a Five Digit Number

Problem Description

Given a five digit integer n where n ranges from 10000 to 99999, your task is to compute and print the sum of its digits. You must not use arrays or strings for this problem. Stick to mathematical operations only. This ensures you understand core operations like modulo and integer division in the C language.

Input Format

A single integer n

Constraints

10000 less than or equal to n less than or equal to 99999

Output Format

An integer representing the sum of all digits in n

Sample Input

10564

Sample Output

16

How It Works

Let us understand how to extract digits from an integer. In C, the modulo operator percent symbol returns the remainder of division. For example 17 modulo 10 equals 7. If you divide 17 by 10 using the slash operator you get 1 which is the truncated integer part. To extract digits from right to left, we repeatedly do n modulo 10 and then update n by dividing it by 10. We continue this loop until n becomes zero. At each step we add the last digit to a sum variable.

Let us take the input 10564 and go through the process step by step


10564 % 10 = 4 → sum = 4
1056 % 10 = 6 → sum = 10
105 % 10 = 5 → sum = 15
10 % 10 = 0 → sum = 15
1 % 10 = 1 → sum = 16
Final sum = 16

Full Code in C


#include <stdio.h>
#include <stdlib.h>

int main() {
    int num, sum = 0;
    scanf("%d", &num);
    while(num != 0) {
        sum += num % 10;
        num /= 10;
    }
    printf("%d", sum);
    return 0;
}

Explanation of the Code

  • We start by including standard headers stdio and stdlib
  • Inside main we declare two integer variables num and sum
  • We use scanf to read the input value from the user
  • We then use a while loop to run until num becomes 0
  • In each loop iteration we extract the last digit using num modulo 10 and add it to sum
  • Then we remove the last digit by dividing num by 10 using integer division
  • Finally we print the result using printf

Test Cases

Input Expected Output
12345 15
11111 5
10000 1
99999 45

Common Mistakes

  • Forgetting to initialize sum to zero before the loop
  • Using float instead of int when dealing with digits
  • Trying to extract digits using strings or arrays for a beginner level problem
  • Missing the condition num not equal to zero in the loop

Beginner Tips from TruongDevs

  • Always test your code with edge inputs like 10000 and 99999
  • Remember integer division in C truncates the result toward zero
  • Use print statements to debug the loop logic step by step if needed

Bonus Challenge

Try modifying the code to accept any positive integer instead of just five digit numbers. This would require removing the constraint and applying the same digit extraction logic using the loop. You can also create a reusable function like int digitSum(int n) that returns the sum. This is good practice for writing modular code in C.

Advanced Extension

Can you modify the code to also print the number of digits in the input number? Hint Use a counter variable and increment it inside the same loop you use for summing digits.

Conclusion

The digit sum problem is a simple yet powerful exercise that teaches core logic building skills in C programming. It introduces you to the use of arithmetic operators, loops, input output functions, and number manipulation without relying on arrays or advanced syntax. By understanding and solving this problem, you are building the foundation for more complex problems. TruongDevs encourages all C beginners to practice this type of logic to become confident programmers.

About the author

TruongDevs
Không phải bug nào cũng xấu, có bug giúp ta tỉnh ra

Post a Comment