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 is define c?

569


What are void pointers in c?

574


What are the 5 elements of structure?

560


What is the difference between local variable and global variable in c?

684


What is an array? What the different types of arrays in c?

654






Explain the difference between null pointer and void pointer.

663


What are header files in c?

614


In a switch statement, what will happen if a break statement is omitted?

600


Explain how can you restore a redirected standard stream?

587


What does sizeof int return?

588


I have written a pro*C program to fetch data from the cursor. where in i have used the concept of BULK FETCH.... each FETCH statement is taking lots of time to fetch specified number of rows at...

9651


What is data types?

636


What are different storage class specifiers in c?

611


Can we initialize extern variable in c?

628


Create a structure to specify data on students given below: Roll number, Name, Department, Course, Year of joining Assume that there are not more than 450 students in the college. 1.write a function to print names of all students who joined in a particular year 2.write a function to print the data of a student whose roll number is given

2511