Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

algorithm for sum of digits?

OpenStudy (anonymous):

There is already a question about that, and some guys answered it, so just search for it. I think the title is sum of two numbers or something like that. Its solved using C/C++.

OpenStudy (anonymous):

u mean 1234 its sum as 10 like this??

OpenStudy (nick67):

@ Zaid123 try yourself thinking that a number is made of digits, each one of them representing a particular power of 10 ...

OpenStudy (anonymous):

@zaid123 its nothing simple in c++,just u have to use divide and remainder function.

OpenStudy (anonymous):

Are there no limits like the input must be at most 9,999? Try thinking. headers main() { int input,thousands,hundreds,tens,ones,sum; printf("Input a number at most 9999"); scanf("%d",&a); thousands = input / 1000; hundreds = input / 100 % 10; tens = input / 10 % 10; ones = input % 10; sum = thousands + hundreds + tens + ones; printf("%d",sum); }

OpenStudy (anonymous):

If someone enter a digit than it will print the some of all the digits.For ex. if number is 6789 than answer will be 30. Solution:- I am using c. void main() { int num,sum=0; //for storing the number. while(num>=1) { int a; // for storing a digit of the number. a=num%10; //we will get a single digit. sum=sum+a; num=num/10; } Ex. if input will be 123. than first time while(123>=1) will be true and a=3,sum=3,num=12. second time while(12>=1) will be satisfied and a=2,sum=5,num=1. third time while(1>=1) will be true. a=1,sum=6,num=0; fourth time while(0>=1) will be false and loop will be terminated.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!