Sum of Digits of a Number in C using While loop | For | Function | Recursion | Without loop
Sum of digits of a number in C using while loop.
In this article, you will learn how to find the sum of digits of a number in C using while loop.
Example
3579
The sum of 3579 digits is = 24
You should have knowledge of the following topics in c programming to understand this program:
- C
main()
function - C
printf()
function - C
while
loop - C
for
loop - C Functions
- C Recursion
Sum of Digits of a Number
In this article, we solve this problem in six methods:
- Using the while loop
- Using the for loop
- Without using loop
- Using the function
- Using the recursion
- Using the do-while loop
Source Code
// Sum of Digits of a Number in C using While loop
#include <stdio.h>
int main() {
int x, sum = 0, m;
printf ("Enter the integer number::\n");
scanf ("%d", &x);
printf ("The sum of %d digits is = ", x);
while (x > 0) {
m = x%10;
sum = sum+m;
x = x/10;
}
printf ("%d\n", sum);
return 0;
}
Output
Enter the integer number::
3579
The sum of 3579 digits is = 24
Sum of Digits of a Number in C using For loop
// Sum of Digits of a Number in C using For loop
#include <stdio.h>
int main() {
int x, sum = 0, m;
printf ("Enter the integer number::\n");
scanf ("%d", &x);
printf ("The sum of %d digits is = ", x);
for (int i = x; i > 0; i = i/10) {
m = i%10;
sum = sum+m;
}
printf ("%d\n", sum);
return 0;
}
Explanation
In this given program, we have taken input 3579
from the user then divide digits of this number and add them to each other to solve this calculation: 3 + 5 + 7 + 9 = 24
.
Then this will return the 24
the final output of the above calculation.
Sum of Digits of a Number in C Without loop
// Sum of Digits of a Number in C Without loop
#include <stdio.h>
int main() {
int x, sum = 0;
printf ("Enter the 3 digit's integer number::\n");
scanf ("%d", &x);
printf ("The sum of %d digits is = ", x);
sum = (x % 10) + ((x / 10) % 10) + (x / 100);
printf ("%d\n", sum);
return 0;
}
Output
Enter the 3 digit's integer number::
987
The sum of 987 digits is = 24
Sum of Digits of a Number in C using Function
// Sum of Digits of a Number in C using Function
#include <stdio.h>
// This function will make sum of digits of number itself
void DigitSum(int x) {
int sum = 0, m;
printf ("The sum of %d digits is = ", x);
while (x > 0) {
m = x%10;
sum = sum+m;
x = x/10;
}
printf ("%d\n", sum);
}
// It's the driver function
int main() {
int x;
printf ("Enter the integer number::\n");
scanf ("%d", &x);
DigitSum(x);
return 0;
}
Output
Enter the integer number::
12345
The sum of 12345 digits is = 15
Sum of Digits of a Number in C using Recursion
// Sum of Digits of a Number in C using Recursion
#include <stdio.h>
// This function will make sum of digits of number itself
// Using the recursion
int DigitSum(int x) {
if (x == 0)
return 0;
return (x % 10 + DigitSum(x / 10));
}
// It's the driver function
int main() {
int x;
printf ("Enter the integer number::\n");
scanf ("%d", &x);
printf ("The sum of %d digits is = ", x);
printf("%d\n", DigitSum(x));
return 0;
}
Sum of Digits of a Number in C using Do While loop
// Sum of Digits of a Number in C using Do While loop
#include <stdio.h>
int main() {
int x, sum = 0, m;
printf ("Enter the integer number::\n");
scanf ("%d", &x);
printf ("The sum of %d digits is = ", x);
do {
m = x%10;
sum = sum+m;
x = x/10;
} while (x > 0);
printf ("%d\n", sum);
return 0;
}
Output
Enter the integer number::
4523
The sum of 4523 digits is = 14
Also, visit these links
C Program to Enter Marks of Five Subjects and Calculate Percentage and Grade
C++ Program to Calculate Percentage and Grade
Java Program to Calculate Grade of students
Python Program to Calculate Total Marks Percentage and Grade of a Student