View All C Programs

 Previews Example  View All C Programs Next Example 

Program to sum of digits of a number in C

This program to sum of digits of a number in cc program to sum the digit of number program first find the digit of number from the reverse order and then sum up this constant into a variable. Let us see how the sum of digit of number is found:
123 = 1 + 2 + 3 = 6
#include <stdio.h>
#include <conio.h>
 main()
{
    int number ,a , b=0;
    printf("Enter the number to number up the digits : ");
    scanf( "%d", &number );

    while(number > 0 )
    { 
         a = number % 10;
         b = b + a ;
         number = number / 10 ;
    }

    printf("Sum of digits = %d", b );
    getch();
}
Result
Enter the number to number up the digits : 123

Sum of digits = 6