Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

How can you convert integers to binary or hexadecimal?

994


Can a variable be both const and volatile?

1093


What is time complexity c?

960


How do I use void main?

1071


What is a struct c#?

1020


What is static identifier?

1188


Why isn't it being handled properly?

1027


Tell me about low level programming languages.

1085


Should a function contain a return statement if it does not return a value?

1043


Write a program to check whether a number is prime or not using c?

1020


Is c++ based on c?

1045


What should malloc(0) do? Return a null pointer or a pointer to 0 bytes?

1039


.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }

1249


Explain what could possibly be the problem if a valid function name such as tolower() is being reported by the c compiler as undefined?

976


What is the explanation for cyclic nature of data types in c?

1160