codescracker


c

C Program to Find and Print Sum of all Digit of any Number



« C Tutorial C Examples »

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

// Write a program in C to print the sum of 
// digit of any given Number by user
// -----------codescracker.com-----------

#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 Code::Blocks IDE, therefore after successful build and run, here is the output:

print sum of even digit c

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

print sum of all digit c

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


« C Tutorial C Examples »






Tools
Calculator

Quick Links
Signup - Login - Give Online Test