Adding digits of a number together

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster

Adding digits of a number together

 
0
  #1
Nov 3rd, 2009
I'm trying to make a function that adds the digits of a number together and finds the sum of them.
For example, for the number 9211, I want to add 9 + 2 + 1 + 1 and output the sum.
Here's my function, I can't figure out where I am going wrong.
Could someone point out why the result isn't coming out correctly?

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int SumFunction (int x)
  5. {
  6. int result = 0;
  7. result = ((x % 10) + ((x / 10) % 10) + ((x / 100) % 10) + ((x / 1000) % 10) + ((x / 10000) % 10));
  8. return (result);
  9.  
  10. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 138
Reputation: restrictment is on a distinguished road 
Solved Threads: 10
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #2
Nov 4th, 2009
Formula is correct. It must be something wrong with your function. Post your whole code.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
0
  #3
Nov 4th, 2009
EDIT: The program worked fine all along. I was just using incorrect input! Sorry, you can delete this thread.
Last edited by Towely; Nov 4th, 2009 at 12:52 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #4
Nov 4th, 2009
Runs fine for me using 1235 and 3459 as input. They're sum buddies and are reported as such. What input is not working for you?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
0
  #5
Nov 4th, 2009
Oh... the problem was with my input! The program worked fine all along!
I'm so dumb. Sorry for the inconvenience.... You guys can delete this thread.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 5
Reputation: suresh1010 is an unknown quantity at this point 
Solved Threads: 0
suresh1010 suresh1010 is offline Offline
Newbie Poster

suresh.a

 
0
  #6
Nov 4th, 2009
dude,
try type casting explicity over the division and mod operation.
The formulae seems to be right. Post ur complete code.









<<BE uniQue
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,464
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
0
  #7
Nov 4th, 2009
>> result = ((x % 10) + ((x / 10) % 10) + ((x / 100) % 10) + ((x / 1000) % 10) + ((x / 10000) % 10));

You know your formula won't handle number above 99999, try
doing this with for loops or recursions.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 78
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training
 
0
  #8
Nov 4th, 2009
To make the program obust you need to use looping concept .
This will provide user the freedom of entering any number
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 3
Reputation: kri_prasad2000 is an unknown quantity at this point 
Solved Threads: 0
kri_prasad2000 kri_prasad2000 is offline Offline
Newbie Poster
 
0
  #9
Nov 4th, 2009
int x=5674876;
int sum=0;
do
{
sum += (x%10);
x /= 10;
}while(x>0);

cout<<"Sum="<<sum;
Last edited by kri_prasad2000; Nov 4th, 2009 at 9:37 am.
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 309 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC