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

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

Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

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

 
0
  #11
Sep 29th, 2008
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void print_int(int number){
  7. stringstream tmp_stream;
  8. tmp_stream << number;
  9. string str_num(tmp_stream.str());
  10. int sum = 0;
  11. for (size_t i = 0; i < str_num.size(); ++i){
  12. cout << str_num[i] << ' ';
  13. sum+=str_num[i]-'0';
  14. }
  15. cout << sum;
  16. }
  17.  
  18. int main(int argc, char **argv) {
  19. int number;
  20. cin >> number;
  21. print_int(number);
  22. return 0;
  23. }
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
  #12
Sep 29th, 2008
Thanks again. One of the problems I did counted as a bonus and was actually worth more than this problem. I understand the solution now, and even though I didn't answer correctly I still scored a 100 because of the bonus question

Thanks!
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
  #13
Sep 30th, 2008
Originally Posted by ivailosp View Post
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void print_int(int number){
  7. stringstream tmp_stream;
  8. tmp_stream << number;
  9. string str_num(tmp_stream.str());
  10. int sum = 0;
  11. for (size_t i = 0; i < str_num.size(); ++i){
  12. cout << str_num[i] << ' ';
  13. sum+=str_num[i]-'0';
  14. }
  15. cout << sum;
  16. }
  17.  
  18. int main(int argc, char **argv) {
  19. int number;
  20. cin >> number;
  21. print_int(number);
  22. return 0;
  23. }

Interesting to see the different solution..heres the one i used, note this is just function not main program to call it

  1. #include <iostream>
  2. #include <cmath>
  3. #include <limits.h>
  4. #include "DigitSeparator.h"
  5.  
  6. void DigitSeparator::Separator( long int x ){
  7. int z;
  8. int q = 1;
  9. int sum = 0;
  10.  
  11. if(x <= ULONG_MAX){
  12. while(1){
  13. if(x < pow(10.0, q++)){
  14. z = q-1;
  15. break;
  16. }
  17. }
  18. while (z != 0) sum += (int)((x%(int)(pow(10.0, z)+0.5))/pow(10.0, --z));
  19. std::cout << sum;
  20. }
  21. std::cin.clear();
  22. std::cout << "\n\n";
  23. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,850
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: 754
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
1
  #14
Sep 30th, 2008
Since we're now giving out alternative solutions, here's mine:
  1. #include <iostream>
  2. #include <iterator>
  3.  
  4. template <typename OutIt>
  5. int sum_digits ( int value, int radix, OutIt *it = 0 )
  6. {
  7. if ( value == 0 )
  8. return 0;
  9.  
  10. int digit = value % radix;
  11. int sum = sum_digits ( value / radix, radix, it ) + digit;
  12.  
  13. if ( it != 0 )
  14. *it++ = digit;
  15.  
  16. return sum;
  17. }
  18.  
  19. int main()
  20. {
  21. std::cout<<"Enter an integer: ";
  22.  
  23. int value;
  24.  
  25. if ( std::cin>> value ) {
  26. int sum = sum_digits ( value, 10,
  27. &std::ostream_iterator<int> ( std::cout, " " ) );
  28.  
  29. std::cout<<"\nThe sum is "<< sum <<'\n';
  30. }
  31. }
New members chased away this month: 4
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
  #15
Sep 30th, 2008
I just like to compare thats all, plus i think its a good learning tool xD
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


Views: 2392 | Replies: 14
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC