C Program to Find Sum of Digits of a Number Using While Loop
Complete guide with code, output, and easy-to-follow explanation for beginners
About the Author
Sankalan Data Technologies
Programming & Data Analytics Tutorials
Published by Sankalan Data Technologies. Our tutorials and data guides are independently developed and curated to provide practical, hands-on learning resources for Python, SQL, and data analytics practitioners.
Quick Answer: Sum of Digits in C
To find the sum of digits of a number, extract each digit using the modulo operator (% 10), add it to a running total, then remove the last digit using division (/ 10). Repeat until the number becomes zero.
#include <stdio.h>
int main() {
int n, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
rem = n % 10; /* get the last digit */
sum = sum + rem; /* add it to the total */
n = n / 10; /* remove the last digit */
}
printf("Sum of digits: %d\n", sum);
return 0;
}
Output:
Enter a number: 421
Sum of digits: 7
What You'll Learn in This Tutorial
- What is the Sum of Digits?
- Logic Behind the Program
- Complete C Program
- Program Output
- Step-by-Step Explanation
- Practice Questions
What is the Sum of Digits?
The sum of digits of a number is the total you get when you add up all of its individual digits. For example, the sum of digits of 123214 is 1 + 2 + 3 + 2 + 1 + 4 = 13.
Here are a few examples:
- 123214 → 1 + 2 + 3 + 2 + 1 + 4 = 13
- 1273 → 1 + 2 + 7 + 3 = 13
- 421 → 4 + 2 + 1 = 7
- 900 → 9 + 0 + 0 = 9
Why Learn This Program?
- Practices while loops — A perfect example of a loop that runs an unknown number of times
- Teaches modulo and division — You learn how to extract digits using basic math
- Common in exams and interviews — Frequently asked in C programming interviews
- Foundation for advanced topics — Useful for palindrome, Armstrong number, and digit-based problems
Logic Behind the Program
The idea is to peel off one digit at a time from the right side of the number and add it to a running total. Here's how it works:
Two Key Operations
1. Extract the last digit: Use n % 10
Example: 421 % 10 = 1
2. Remove the last digit: Use n / 10
Example: 421 / 10 = 42
By repeating these two steps, we can go through every digit of the number and add them up.
Tip: Remember that % gives the remainder and / gives the quotient. These two operators are the heart of this program.
Complete C Program
C Program to Find Sum of Digits Using While Loop
/**
* C program to find the sum of digits of a given number
* using a while loop
*/
#include <stdio.h>
int main() {
int n, sum = 0, rem, n1;
/* Ask the user to enter a number */
printf("Enter a number: ");
scanf("%d", &n);
/* Save the original number for display */
n1 = n;
/* Loop until all digits are processed */
while (n != 0) {
rem = n % 10; /* get the last digit */
sum = sum + rem; /* add it to the sum */
n = n / 10; /* remove the last digit */
}
/* Display the result */
printf("Sum of digits of %d is %d\n", n1, sum);
return 0;
}
Program Output
When the user enters 421, the program prints:
Enter a number: 421
Sum of digits of 421 is 7
When the user enters 123214, the pattern becomes:
Enter a number: 123214
Sum of digits of 123214 is 13
Step-by-Step Explanation
-
Declare variables: We use
nfor the input number,sumto store the running total (initialized to 0),remfor the extracted digit, andn1to save the original number. -
Read input: The program asks the user for a number and reads it using
scanf(). -
Save the original: We copy
ninton1so we can display the original number later. -
Start the while loop: The loop runs as long as
n != 0. Each iteration handles one digit. -
Extract the last digit:
rem = n % 10gives the rightmost digit. -
Add to sum:
sum = sum + remadds the digit to the running total. -
Remove the last digit:
n = n / 10chops off the rightmost digit. - Repeat: Steps 5-7 repeat until the number becomes 0.
-
Display result: After the loop, we print the sum using
printf().
Visual Walkthrough (n = 421)
Start: n = 421, sum = 0 Iteration 1: rem = 421 % 10 = 1 sum = 0 + 1 = 1 n = 421 / 10 = 42 Iteration 2: rem = 42 % 10 = 2 sum = 1 + 2 = 3 n = 42 / 10 = 4 Iteration 3: rem = 4 % 10 = 4 sum = 3 + 4 = 7 n = 4 / 10 = 0 Loop ends. Final sum = 7.
Common Mistakes to Avoid
- Forgetting to update
n— If you don't don = n / 10, the loop runs forever. - Forgetting to initialize
sumto 0 — Otherwise, the sum starts with garbage values. - Using
%instead of/— Modulo gives remainder; division gives quotient. Make sure you use the right one. - Overwriting the original number — Save it in another variable (
n1) if you want to print it later. - Using
n < 0in the loop — The condition should ben != 0to handle both positive and negative numbers.
Practice Questions
Once you understand this program, try these variations:
- Find the sum of digits using a for loop instead of while.
- Find the product of digits instead of the sum.
- Count the total number of digits in the number.
- Find the sum of only even digits.
- Find the sum of digits without using % and / (using string conversion).
- Write the same program using recursion.
Hint: For most variations, the main loop logic stays the same. Only the operation inside the loop changes.
Why This Program Matters
- Foundation for digit-based problems: Palindrome, Armstrong number, and reverse-a-number all use the same digit extraction logic.
- Common in exams: Digit problems are asked in BCA, MCA, and engineering practical exams.
- Interview favourite: Interviewers often ask you to modify this program to solve related problems.
- Real-world use: Digit sums are used in checksum algorithms and digital validation.
Frequently Asked Questions
How do I find the sum of digits of a number in C?
To find the sum of digits, extract each digit using the modulo operator (% 10) and add it to a running total. Then divide the number by 10 to remove the last digit. Repeat until the number becomes zero.
Why do we use n % 10 and n / 10 in the sum of digits program?
The modulo operator (n % 10) gives the last digit of the number. Dividing by 10 (n / 10) removes the last digit. Together they let us process each digit one by one.
What is the sum of digits of 123214?
The sum of digits of 123214 is 1 + 2 + 3 + 2 + 1 + 4 = 13.
Can I find the sum of digits without a loop?
Yes, you can use recursion or convert the number to a string and add each character's numeric value. However, the while loop is the most straightforward approach for beginners.
What happens if the number is negative?
If the number is negative, you should convert it to positive first using abs() from math.h, or handle the sign separately. Otherwise, the modulo operator may give negative remainders.
What is the sum of digits of 0?
The sum of digits of 0 is 0. In the program, if the user enters 0, the while loop never runs, so the sum stays at its initial value of 0.
Related Tutorials
Previous Topic: Check if a Number is Palindrome Using While Loop | Next Topic: Print Hollow Square Pattern Using While Loop