How do I convert an integer into digits and add their sum?

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

Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

How do I convert an integer into digits and add their sum?

 
0
  #1
Sep 29th, 2008
I'm working on an assignment for Computer Science 121. The assignment is to write a program that prompts a user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6 and the sum as 18. I can't seem to get it working properly; I think my algorithm is wrong. I tried rewriting the algorithm, but I keep getting stuck in it. Can someone help me out? Here's what I have so far:

  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int num, num2, sum = 0, dig;
  9.  
  10. cout << "Please enter a number" << endl;
  11. cin >> num;
  12.  
  13. if(num < 0)
  14. num = -num;
  15. num2 = num;
  16. dig = num;
  17. do
  18. {
  19. do
  20. {
  21. dig = dig / 10;
  22. }
  23. while (dig >= 10);
  24.  
  25. cout << dig << " ";
  26.  
  27. if (num > 100)
  28. num = num - (100*(num/100));
  29. else
  30. num = num - (10*(num/10));
  31.  
  32. sum = sum + num2 % 10;
  33.  
  34. num2 = num2 / 10;
  35.  
  36. dig = num;
  37.  
  38. }
  39. while (num2 > 0);
  40. cout << endl;
  41. cout << "The sum of the digits is " << sum << endl;
  42.  
  43. return 0;
  44. }

Thank you for the help
Last edited by ShadowOfBlood; Sep 29th, 2008 at 2:03 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How do I convert an integer into digits and add their sum?

 
0
  #2
Sep 29th, 2008
A combination of modulus division and normal division will do this nicely for you.

You have way to many steps, you can retreive each number in 1 line of code using both normal and modulus division.

its all to do with 10^x in a way xD

Chris
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,734
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 738
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How do I convert an integer into digits and add their sum?

 
1
  #3
Sep 29th, 2008
Start with the summation, because that's easy. You already seem to know about taking the remainder of ten to get the least significant digit and dividing by ten to slice it off. Remember to keep it as simple as possible. When you do this with more than one loop, you're not keeping it simple:
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. std::cout<<"Enter a number: ";
  6.  
  7. int num;
  8.  
  9. if ( std::cin>> num ) {
  10. int sum = 0;
  11.  
  12. if ( num < 0 )
  13. num = -num;
  14.  
  15. while ( num != 0 ) {
  16. sum += num % 10;
  17. num /= 10;
  18. }
  19.  
  20. std::cout<<"The sum is "<< sum <<'\n';
  21. }
  22. }
The fun part is getting the digits to print out from most significant to least significant, as per your example using 3456. If you just print num % 10 in the while loop, you can get the digits in reverse. To print in the right order, you need to store the digits somewhere (the simpler solution, IMO), or extract the digits from most significant to least significant.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How do I convert an integer into digits and add their sum?

 
0
  #4
Sep 29th, 2008
Thanks, I'll try it
Last edited by ShadowOfBlood; Sep 29th, 2008 at 2:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How do I convert an integer into digits and add their sum?

 
0
  #5
Sep 29th, 2008
Originally Posted by Narue
The fun part is getting the digits to print out from most significant to least significant, as per your example using 3456. If you just print num % 10 in the while loop, you can get the digits in reverse. To print in the right order, you need to store the digits somewhere (the simpler solution, IMO), or extract the digits from most significant to least significant.
This is what i was thinking about from the go was to get them in the correct order, so maybe my post may give you a small clue
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How do I convert an integer into digits and add their sum?

 
0
  #6
Sep 29th, 2008
Thanks for the help, but I still can't seem to grasp how to output the digits in order. I can get them to output in reverse just fine, but when I try to get them to output in order I can't seem to figure it out. I just started learning c++ and just learned loops, so I don't know much code. Can I get some more clues? My Computer Science class starts in 15 minutes (last minute, I know >< Thought I could figure it out on my own).
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How do I convert an integer into digits and add their sum?

 
0
  #7
Sep 29th, 2008
You could store all the numbers in an array as suggested and then print them out, or you can do something like this...

num = 546

mostsigdigit = (546 % 10^3)/10^2


probably gave too much away there but oh well
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How do I convert an integer into digits and add their sum?

 
0
  #8
Sep 29th, 2008
But what if the user enters a number with more than 3 digits?

Oh, and I haven't learned arrays yet :\
Last edited by ShadowOfBlood; Sep 29th, 2008 at 2:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: ShadowOfBlood is an unknown quantity at this point 
Solved Threads: 0
ShadowOfBlood ShadowOfBlood is offline Offline
Newbie Poster

Re: How do I convert an integer into digits and add their sum?

 
0
  #9
Sep 29th, 2008
Thanks for the help
I have to get to class now.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 671
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: How do I convert an integer into digits and add their sum?

 
0
  #10
Sep 29th, 2008
Well it would be

(546 % 10^x)/10^(x-1)

where x = number of digits in number
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC