Programming Examples
Accept a number and find sum of its individual digits repeatedly till the result is a single digit.
Write a program to accept a number and find sum of its individual digits repeatedly till the result is a single digit.
Solution
# include <stdio.h>
void main()
{
int n,s=0;
clrscr();
printf(“\n Enter a Number :”);
scanf(“%d”,&n);
printf(“\n Sum of Digits till a single digit\n %d”,n);
for(; n!=0 ;)
{
s=s+n%10;
n=n/10;
if(n==0 && s>9)
{printf(“\n %2d”,s);
n=s;
s=0;
}
}
printf(“\n %2d”,s);
getch();
}
Output
OUTPUT:
Enter a Number :4687
Sum of Digits till a single digit
4687
25
7