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

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz