C Program to Find Sum of Digit of Number Until it Reduces to Single Digit
In this C program, we are going to find sum of digit of a given number until it reduces to single digit. For Example, if 9999974 is given number then while finding sum of digit at first step we get 56 i.e. 9+9+9+9+9+7+4 = 56, similarly 56 is two digit number we again find sum of digit i.e. 5+6=11 and 11 is again two digit we find sum i.e. 1+1=2. And finally we get single digit i.e. 2 and then we stop this process.
Program
#include<stdio.h>
#include<conio.h>
int main()
{
long int number, sum, step=1, rem;
clrscr();
printf("Enter number: ");
scanf("%ld", &number);
do
{
sum = 0;
while(number!=0)
{
rem = number%10;
sum = sum + rem;
number = number/10;
}
printf("Step-%ld Sum = %ld\n", step, sum);
number = sum;
step = step+1;
}while(number>9);
getch();
return(0);
}
Output of above program :
Run 1: -------------- Enter number: 9999974 ↲ Step-1 Sum = 56 Step-2 Sum = 11 Step-3 Sum = 2 Run 2: -------------- Enter number: 8 ↲ Step-1 Sum = 8 Note: ↲ indicates ENTER is pressed.