Program to find the sum of digits of a given number until
the sum becomes a single digit

Answer Posted / ashutosh shashi

int n = 123456789; //any numer of you want sum
int sum = 0;
while (n > 0)
{
int p = n % 10;
sum = sum + p;
n = n / 10;
if(n==0 && sum>9)
{
n=sum;
sum=0;
}
}
printf("%d",sum);

Is This Answer Correct ?    157 Yes 63 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the maximum length of an identifier?

774


Why main function is special give two reasons?

1049


Write a programme using structure that create a record of students. The user allow to add a record and delete a record and also show the records in ascending order.

1712


the statement while(i) puts the entire logic in loop. this loop is called a) indefinite loop b) definite loop c) loop syntax wrong d) none of the above

701


Can we assign integer value to char in c?

718






How pointers are declared?

639


What is null in c?

678


What is the function of this pointer?

773


Define Spanning-Tree Protocol (STP)

752


What are different storage class specifiers in c?

753


write a program to concatenation the string using switch case?

1665


What does stand for?

705


What are the different types of control structures?

677


What is nested structure?

670


Are there any problems with performing mathematical operations on different variable types?

666