how to find sum of digits in C?

Answer Posted / keerthireddy

#include<stdio.h>
#include<conio.h>
main()
{
int n,i,sum=0,r;
printf("enter the value");
scanf("%d",&n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("sum = %d",sum);
}
output:
3456
how it works as follows:
3456%10 means it gives reminder as 6
6 will be added to the sum
3456/10 means it gives quotient as 345
then again loop is executing until the n value is 0
finally the result as 6+5+4+3=18

Is This Answer Correct ?    9 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are actual arguments?

647


What is the explanation for modular programming?

686


What are header files in c?

618


What is a program flowchart and how does it help in writing a program?

664


main() { inta=10,b=20; a>=5?b=100:b=200; printf("%d ",b); }

912






What is the difference between Printf(..) and sprint(...) ?

789


FILE PROGRAMMING

1779


What is a constant and types of constants in c?

604


Explain a pre-processor and its advantages.

633


How can you read a directory in a C program?

651


What are lookup tables in c?

550


Describe the order of precedence with regards to operators in C.

633


Give differences between - new and malloc() , delete and free() ?

612


What language is lisp written in?

618


What is a pragma?

670