943,880 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4689
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 29th, 2008
0

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

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Sep 29th, 2008
0

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

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Sep 29th, 2008
1

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

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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 29th, 2008
0

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

Thanks, I'll try it
Last edited by ShadowOfBlood; Sep 29th, 2008 at 2:10 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Sep 29th, 2008
0

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

Quote 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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Sep 29th, 2008
0

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

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).
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Sep 29th, 2008
0

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

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Sep 29th, 2008
0

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

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Sep 29th, 2008
0

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

Thanks for the help
I have to get to class now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ShadowOfBlood is offline Offline
21 posts
since Sep 2008
Sep 29th, 2008
0

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

Well it would be

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

where x = number of digits in number
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Checking string for letters / numbers
Next Thread in C++ Forum Timeline: Helpppp.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC