943,949 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4690
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 29th, 2008
0

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

cpp Syntax (Toggle Plain Text)
  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. }
Reputation Points: 21
Solved Threads: 22
Junior Poster
ivailosp is offline Offline
129 posts
since Apr 2008
Sep 29th, 2008
0

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

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

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

Click to Expand / Collapse  Quote originally posted by ivailosp ...
cpp Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Sep 30th, 2008
1

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

Since we're now giving out alternative solutions, here's mine:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Sep 30th, 2008
0

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

I just like to compare thats all, plus i think its a good learning tool xD
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jul 18th, 2010
-1
Re: How do I convert an integer into digits and add their sum?
Here is my code
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. void main()
  5. {
  6. int number , num ;
  7. double count;
  8.  
  9. cout<<"Please enter an integer."<<endl;
  10. cin>>number;
  11.  
  12. if(number < 0) // Convert negative integers to positive integers
  13. number = -number;
  14.  
  15. num = number;
  16.  
  17. if(number < 10) //Case1: Integers less than 10
  18. cout<<number<<endl;
  19.  
  20.  
  21. while(num > 10)//Case2: Integers greater than 10
  22. {
  23. for(count = 0 ; num > 10 ; count++)
  24. {
  25. num = num / 10;
  26. }
  27. cout<<num<<" ";
  28. number = number - ( num * pow(10 ,count) );
  29. num = number;
  30. }
  31. cout<<num<<" "; //Prints the last digit
  32.  
  33. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andro23 is offline Offline
5 posts
since Jul 2010
Jul 19th, 2010
0
Re: How do I convert an integer into digits and add their sum?
and another one :
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <iterator>
  6.  
  7. using namespace std;
  8.  
  9. int main(){
  10. string num;
  11. cin >> num;
  12. //print each digit
  13. std::copy( num.begin(), num.end(), std::ostream_iterator<char>(cout," "));
  14. //get sum
  15. int sum = std::accumulate(num.begin(),num.end(),0) - '0' * num.size();
  16.  
  17. cout << "sum is = " << sum << endl;
  18.  
  19. return 0;
  20. }

Yes I know, no error checking. Thats because the user knows better, than to mess with me or my program.
Last edited by firstPerson; Jul 19th, 2010 at 2:12 am.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Jul 25th, 2010
0
Re: How do I convert an integer into digits and add their sum?
Another method using cin.get
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. void main()
  4. {
  5. int sum=0;
  6. char ch;
  7. cout<<"Please enter an integer."<<endl;
  8.  
  9. for (int index=0; ;index++)
  10. {
  11. cin.get(ch);
  12.  
  13. if(ch==' ' ||ch =='\n' || ch=='\t') //To end the loop when all the input is read.
  14. {
  15. cout<<endl;
  16. break;
  17. }
  18.  
  19. if (index==0) //To print "Number: " before printing the input
  20. cout<<"Number: ";
  21.  
  22. if(ch=='-')
  23. continue;
  24.  
  25. sum = sum + (ch-48); //Minus 48 (See ASCCI table http://www.cpptutor.com/ascii.htm)
  26. cout<<ch<<" ";
  27. }
  28. cout<<"Sum = "<<sum<<endl;
  29. }
Last edited by andro23; Jul 25th, 2010 at 6:39 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andro23 is offline Offline
5 posts
since Jul 2010
Jul 25th, 2010
0
Re: How do I convert an integer into digits and add their sum?
Another method using string (Sorry I wasn't able to get the sum of the numbers. It could use atoi() to convert from string type to int type but i wasn;t able to understand this atoi() )
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. void main()
  5. {
  6. int sum=0;
  7. string number , b ;
  8.  
  9. cout<<"Please enter an integer."<<endl;
  10. cin>>number;
  11.  
  12. for(int index=0 ; ; index++)
  13. {
  14. string b(number,index, 1);
  15.  
  16. if(b=="-")
  17. continue;
  18.  
  19. if(b =="")//To end the loop when all the input is printed.
  20. break;
  21.  
  22. cout<<b<<" ";
  23. }
  24. cout<<endl;
  25. }
Last edited by andro23; Jul 25th, 2010 at 7:39 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
andro23 is offline Offline
5 posts
since Jul 2010
Jul 26th, 2010
0
Re: How do I convert an integer into digits and add their sum?
I have modified my solution to a challenge question given by firstPerson to reverse number and add without trailing zeros. The new solution is now adding digits of number only:
C++ Syntax (Toggle Plain Text)
  1. int main(){unsigned d=0,n,s=0;cin>>n;for(;n;d=n%10,s+=d,n/=10);cout<<s;return s;}
Ok, I must apologize because this code is not well readable. My idea was finding very short program code.
Last edited by 1stDAN; Jul 26th, 2010 at 10:29 am.
Reputation Points: 22
Solved Threads: 3
Light Poster
1stDAN is offline Offline
26 posts
since Jul 2010

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