How to Calculate the Sum of Digits by C Program

sum of digits by c programing language

Introduction :

Calculating the sum of digits of a given number is a common problem in programming. Using C programing language, you can calculate the sum of digits of a given integer number using simple mathematical calculation. First, you have to understand that what is the sum of digits. The sum of digits of a number is the sum of all its digits by which the number created.

Here, you have to extract each digit of a number and sum them up. For example, the sum of digits of the number 4378 would be 4+3+7+8=22. In this article, I shall discuss with you how to calculate the sum of digits of a given integer number in C programming language.

About the program :

After running the program, it asks you to enter an integer number. Then, it calculates the sum of all the digits of the given integer. Finally, it prints the result on the console screen.

Explanation of the program :

At first, include stdio.h header file in the program. Now, declare variables such as “input_number”, “copy_of_input” and “total_sum” in the main() function. Now, ask the user to enter an integer number using the printf() function. You can use scanf() function to get the user input and store in “input_number” variable. Now, copy the original value of “input_number” in “copy_of_input” variable.

Then, run a while loop that continues as long as “input_number” is not zero (0). After that, extract each digit from the input number using the modulus (%) operator. To extract the last digit of the number, use the modulus operator with the number 10. Then, remove the last digit of the number using division (/) operator. Here, you have to divide the number by 10.

This process continues until all the digits have been extracted and adding each digit to “total_sum” variable. When all the digits have been extracted and added together, print the sum of digits using the printf() function.

How run the program :

To run this program, open VS Code. If you do not install VS Code on your PC, click here. After open VS Code, create a new C file like “sum_of_digits.c”. Now, copy the below code and paste in the C file that you have just created. Then, save it and run the program on your PC.

Source code of the program :

The following code is the source code of calculating the sum of digits program in C programming language.

/* Developd by Puskar Jasu*/
#include <stdio.h>
int main()
{
    int input_number, copy_of_input, total_sum = 0;
    printf("Enter a number for find sum of all digits\n");
    scanf("%d", &input_number);
    copy_of_input = input_number;
    while (input_number != 0)
    {
        total_sum = total_sum + input_number % 10;
        input_number = input_number / 10;
    }
    printf("Sum of all digits of %d is %d\n", copy_of_input, total_sum);
    return 0;
}

Output of the program :

Now, run the code on your PC and see the following output like below image.

Output of calculate the sum of digits by C program

Conclusion :

In this article, I have discussed how to calculate the sum of digits by C programming language. I think you would be understood how extracting each digit by modulus operator, adding it to a variable and then divide the number by 10 until all the digits have been extracted. Lastly, print the sum of all digits on the screen. Thank you for visiting my site.

Scroll to Top