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 volatile variable in c with example?

590


A global variable when referred to in another file is declared as this a) local variable b) external variable c) constant d) pointers

651


What are the different file extensions involved when programming in C?

764


Write a program to print factorial of given number using recursion?

607


Explain how do you determine a file’s attributes?

596






Hello. How to write a C program to check and display president party like if i type in the console "biden" and hit enter the output shoud be : "biden is democrat" and if i type "trump" and hit enter the output shoud be: "trump is republican"

1584


What is getch c?

860


How old is c programming language?

583


What is a pointer and how it is initialized?

611


What is void c?

571


Can we assign string to char pointer?

591


What is NULL pointer?

680


Differentiate between full, complete & perfect binary trees.

675


What are external variables in c?

549


Tell me what is null pointer in c?

617