Finding the sum of digits of a number in c is a basic yet important concept in programming. This involves breaking down a number into its individual digits and adding them together. It's a great exercise for learning loops and basic arithmetic operations in C.
Now let’s look at two examples:
For the number 345: The digits are 3, 4, and 5.
The sum of the digits is 3 + 4 + 5 = 12.
For the number 1289: The digits are 1, 2, 8, and 9.
The sum of the digits is 1 + 2 + 8 + 9 = 20.
C Program to Find Sum of Digits of a Number Using For Loop
The c program to find sum of digits of a number can be written in a few different ways. The algorithm for it is as follows:
- Take a number as input.
- Divide the number by 10 and store the remainder in a variable.
- Add this remainder to a running sum.
- Remove the last digit by dividing the number by 10.
- Repeat steps 2 to 4 until the number becomes 0.
- Print the final sum.
C Program to Find Sum of Digits of a Number Using While Loop or Iterative Approach
Here’s how to do it using a while loop:
/* C program to find sum of digits of a number */
#include <stdio.h>
int main(void)
{
int num, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &num);
// Keep dividing until the number is not zero
while (num != 0)
{
rem = num % 10;
sum = sum + rem;
num = num / 10;
}
printf("Sum of digits of the number is %d", sum);
return 0;
}
Output:
Enter a number: 10044
Sum of digits of the number is 9
=== Code Execution Successful ===
How the Code Works:
User Input: The program prompts the user to enter and store a number in num.
Initialisation: sum is initialised to 0 to store the running total, and rem is used to store the remainder (last digit).
Loop Execution: The while loop runs as long as num is not 0.
In each iteration: The last digit is obtained using num % 10 and stored in rem. Then rem is added to sum. The last digit is removed by updating num to num / 10.
Output: The final sum of the digits is printed once the loop completes.
C Program to Find Sum of Digits of a Number Using for Loop
Now let’s take a look at how to find the sum of the digits of a number in C using for loop:
#include <stdio.h>
int main(void)
{
int num, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &num);
// Loop to calculate sum of digits
for (; num != 0; num /= 10)
{
rem = num % 10; // Extract the last digit
sum += rem; // Add it to the sum
}
printf("Sum of digits of the number is %d", sum);
return 0;
}
Output:
Enter a number: 123564
Sum of digits of the number is 21
=== Code Execution Successful ===
How the Code Works
- User Input: The program asks the user to input a number, which is stored in num.
- For Loop: The for loop continues as long as num is not zero.
- In each iteration:
- The last digit is extracted using num % 10.
- This digit is added to sum.
- The number is reduced by dividing num by 10.
- Output: After the loop ends, the final sum of digits is printed.
C Program to Find Sum of Digits of a Number Using char Input
Now let’s look at the sum of the digits of a number in c using char input:
#include <stdio.h>
int main(void)
{
char num[20];
int sum = 0;
printf("Enter a number: ");
scanf("%s", num);
// Iterate through each character until the null terminator
for (int i = 0; num[i] != '\0'; i++)
{
sum += num[i] - '0'; // Convert character to digit and add to sum
}
printf("Sum of digits of the number is %d", sum);
return 0;
}
Output:
Enter a number: 12345
Sum of digits of the number is 15
=== Code Execution Successful ===
How the Code Works
- User Input: The user enters a number as a string, stored in the char array num.
- Initialisation: sum is initialised to 0 to store the total sum of digits.
- Loop Execution:
- A for loop iterates through each character of num until the null terminator '\0' is reached.
- Each character digit is converted to an integer using num[i] - '0'.
- The converted digit is added to the sum.
- Output: The final sum of the digits is printed after the loop completes.
C Program to Find Sum of Digits of a Number Using Recursion
Now let’s look at the c program to find sum of digits of a number using recursion:
/* Find the sum of digits recursively */
#include <stdio.h>
long sum_of_digits_recur(long n)
{
if (n == 0)
return 0;
else
return n % 10 + sum_of_digits_recur(n / 10);
}
int main(void)
{
long n;
printf("Enter a number: ");
scanf("%ld", &n);
printf("Sum of digits of the number is %ld", sum_of_digits_recur(n));
return 0;
}
Output:
Enter a number: 12445
Sum of digits of the number is 16
=== Code Execution Successful ===
How the Code Works
- Function sum_of_digits_recur: This function calculates the sum of digits using recursion.
- Base Case: If n equals 0, the function returns 0 and stops further recursion.
- Recursive Case: The function returns the last digit (n % 10) added to the result of a recursive call with the remaining digits (n / 10). This continues until n becomes 0.
- Main Function:
- Prompts the user to enter a number and stores it in n.
- Calls sum_of_digits_recur(n) to calculate the sum of digits.
- Print the result.
C Program to Find Sum of Digits of a Number Using Input Number as String
Now let’s look at c program to find sum of digits of a number using input number as string:
#include <stdio.h>
#include <string.h>
int main(void)
{
char num[50];
int sum = 0;
printf("Enter a number: ");
scanf("%s", num);
// Loop through each character in the string
for (int i = 0; i < strlen(num); i++)
{
sum += num[i] - '0'; // Convert character to digit and add to sum
}
printf("Sum of digits of the number is %d", sum);
return 0;
}
Output:
Enter a number: 12568
Sum of digits of the number is 22
=== Code Execution Successful ===
How the Code Works
- User Input: The user enters a number, which is stored as a string in the char array num.
- For Loop:
- Iterates through each character in the string.
- Converts each character to its corresponding integer value using num[i] - '0'.
- Adds the integer value to the sum.
Output: Once the loop completes, the program prints the total sum of the digits.
Conclusion
Learning how to find sum of digits of a number in c in ways helps students build a solid foundation in programming. This exercise reinforces core concepts like loops, recursion, and character manipulation. These techniques are essential for solving more complex problems efficiently. Mastering such basics sharpens problem-solving skills and logical thinking, which are crucial for becoming successful software professionals. To start your journey into software development and become job ready, enroll into the CCBP Academy 4.0 program today!
Boost Your Placement Chances by Learning Industry-Relevant Skills While in College!
Explore ProgramFrequently Asked Questions
1. What is the purpose of the sum of digits program?
The sum of digits program explains how to convert a number into individual digits and add them. This is valuable to learning more about loops, recursion and basic computations.
2. Is it possible to write the sum of digits program using recursion?
Yes, the sum of digits can also be defined recursively. After dividing the number by 10, taking the remainder, and arriving at 0, the recursive function sums the remainder until the number equals 0.
3. What is the mechanism of operation of the sum of digits approach based on character?
In the character-based methods the number is actually handled as a string of characters. A character is converted to an integer, and then the sum is found by using a loop on the string.
4. Which method is better for finding the sum of digits: loop or recursion?
Both methods are equally correct, but general looping structures have been found to be more efficient in terms of memory utilisation. Recursion, in particular, is a good technique that one must study to encounter more complicated problems and learn problem-solving skills.
5. Is it possible to count the number of digits in large numbers?
Yes it can be done but number can be handle in larger number by using data types like long.