Sum of Digits of a Number in C Program using For loop | While | Do-While loop | Function | Recursion
ADVERTISEMENTS
Sum of digits of a number in c program.
In this article, you will learn how to make the sum of digits of a number in c by using various approaches like: for loop, while loop, do-while loop, function, and recursion.
INPUT:
1234
==> 1 + 2 + 3 + 4
=> 10
Example-1
Enter Number: 9876
Output: 30
Example-2
Enter Number: 1298
Output: 20
You should know about the following topics in C programming to understand this program:
- For loop in C
- While loop in C
- Do-while loop in C
- C Function
- C Recursion
1 - 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>
// It's the main function of the program
int main()
{
int num, sum = 0, mod;
// Step-1 Enter a Number
printf("Enter Number: ");
scanf("%d", &num);
// Step-2 Iterate over the input number to get each digit and make the sum of each digit
for (int n = num; n > 0; n = n / 10)
{
mod = n % 10;
sum += mod;
}
// Step-3 Final output of the program
printf("\nOutput: %d", sum);
return 0;
}
Output
Enter Number: 9876
Output: 30
2 - Sum of Digits of a Number in C using While loop
// Sum of Digits of a Number in C using While loop
#include <stdio.h>
// It's the main function of the program
int main()
{
int num, sum = 0, mod;
// Step-1 Enter a Number
printf("Enter Number: ");
scanf("%d", &num);
// Step-2 Iterate over the input number to get each digit and make the sum of each digit
while (num > 0)
{
mod = num % 10;
sum += mod;
num = num / 10;
}
// Step-3 Final output of the program
printf("\nOutput: %d", sum);
return 0;
}
3 - 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>
// It's the main function of the program
int main()
{
int num, sum = 0, mod;
// Step-1 Enter a Number
printf("Enter Number: ");
scanf("%d", &num);
// Step-2 Iterate over the input number to get each digit and make the sum of each digit
do
{
mod = num % 10;
sum += mod;
num = num / 10;
} while (num > 0);
// Step-3 Final output of the program
printf("\nOutput: %d", sum);
return 0;
}
Output
Enter Number: 654
Output: 15
4 - Sum of Digits of a Number in C using Function
// Sum of Digits of a Number in C using Function
#include <stdio.h>
// Function to make sum of each digits
int sum_of_digits(int num)
{
int sum = 0, mod;
while (num > 0)
{
mod = num % 10;
sum += mod;
num = num / 10;
}
return sum;
}
// It's the main function of the program
int main()
{
int num;
// Step-1 Enter a Number
printf("Enter Number: ");
scanf("%d", &num);
// Step-2 Call the sum_of_digits() function to get the sum of each digits
// & print the final output of the program
printf("\nOutput: %d", sum_of_digits(num));
return 0;
}
5 - Sum of Digits of a Number in C using Recursion
// Sum of Digits of a Number in C using Recursion
#include <stdio.h>
// Recursive function to make the sum of each digit
int rec_sum_digits(int num, int sum, int mod)
{
mod = num % 10;
sum += mod;
num = num / 10;
if (num > 0)
rec_sum_digits(num, sum, mod);
else
return sum;
}
// It's the main function of the program
int main()
{
int num, sum = 0, mod = 0;
// Step-1 Enter a Number
printf("Enter Number: ");
scanf("%d", &num);
// Step-2 Call the recursive function rec_sum_digits() to get the sum of each digits
printf("\nOutput: %d", rec_sum_digits(num, sum, mod));
return 0;
}
Output
Enter Number: 1298
Output: 20