C program to find and print the sum of all the digits of any number

Here we will learn about how to create a program in C that will ask the user to enter any number (at run-time) as input and then calculate and print the sum of all the digits present in that given number. Here is the program:

#include<stdio.h>
#include<conio.h>
int main()
{
    int num, sum=0, rem;
    printf("Enter any number: ");
    scanf("%d", &num);
    while(num>0)
    {
        rem = num%10;
        sum = sum + rem;
        num = num/10;
    }
    printf("\nSum of Digit = %d", sum);
    getch();
    return 0;
}

As the program was written under the Code::Blocks IDE, therefore, after a successful build and run, here is the output:

print sum of even digit c

Supply any number, say 2358, and press the ENTER key to see the sum of all the digits as the output or result. Here is the second snapshot of the sample run:

print sum of all digit c

Here is a list of some of the main steps used in the above program:

C Quiz


« Previous Program Next Program »


Liked this post? Share it!