Thursday, 24 July 2014

C program to find the sum of individual digits of a number repeatedly till the sum is a single digit number

/* Program to find the sum of individual digits of a number repeatedly till the sum is a single digit number */

#include<stdio.h>
#include<conio.h>
void main()
{
 int n,rem,sum=0;
 clrscr();
 printf("Enter a number:");
 scanf("%d",&n);
 printf("Entered Number is:%d",n);

 cal: while(n!=0)
{
 rem=n%10;
 sum+=rem;
 n=n/10;
}
 if(sum>9)
   {
    n=sum;
    sum=0;
    goto cal;
   }
 else
    printf("\n\nSingle digit number is:%d",sum);
 getch();
}

No comments:

Post a Comment