Adding digits of a number together

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 29
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
23 Days Ago
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: 128
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #2
23 Days Ago
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: 29
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
0
  #3
23 Days Ago
EDIT: The program worked fine all along. I was just using incorrect input! Sorry, you can delete this thread.
Last edited by Towely; 23 Days Ago at 12:52 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
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: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #4
23 Days Ago
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: 29
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
0
  #5
23 Days Ago
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
23 Days Ago
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,142
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 144
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
0
  #7
23 Days Ago
>> 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.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 77
Reputation: rati is an unknown quantity at this point 
Solved Threads: 0
rati rati is offline Offline
Junior Poster in Training
 
0
  #8
23 Days Ago
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
23 Days Ago
int x=5674876;
int sum=0;
do
{
sum += (x%10);
x /= 10;
}while(x>0);

cout<<"Sum="<<sum;
Last edited by kri_prasad2000; 23 Days Ago at 9:37 am.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC