| | |
How do I convert an integer into digits and add their sum?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 129
Reputation:
Solved Threads: 22
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> using namespace std; void print_int(int number){ stringstream tmp_stream; tmp_stream << number; string str_num(tmp_stream.str()); int sum = 0; for (size_t i = 0; i < str_num.size(); ++i){ cout << str_num[i] << ' '; sum+=str_num[i]-'0'; } cout << sum; } int main(int argc, char **argv) { int number; cin >> number; print_int(number); return 0; }
•
•
•
•
cpp Syntax (Toggle Plain Text)
#include <iostream> #include <sstream> #include <string> using namespace std; void print_int(int number){ stringstream tmp_stream; tmp_stream << number; string str_num(tmp_stream.str()); int sum = 0; for (size_t i = 0; i < str_num.size(); ++i){ cout << str_num[i] << ' '; sum+=str_num[i]-'0'; } cout << sum; } int main(int argc, char **argv) { int number; cin >> number; print_int(number); return 0; }
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)
#include <iostream> #include <cmath> #include <limits.h> #include "DigitSeparator.h" void DigitSeparator::Separator( long int x ){ int z; int q = 1; int sum = 0; if(x <= ULONG_MAX){ while(1){ if(x < pow(10.0, q++)){ z = q-1; break; } } while (z != 0) sum += (int)((x%(int)(pow(10.0, z)+0.5))/pow(10.0, --z)); std::cout << sum; } std::cin.clear(); std::cout << "\n\n"; }
Since we're now giving out alternative solutions, here's mine:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iterator> template <typename OutIt> int sum_digits ( int value, int radix, OutIt *it = 0 ) { if ( value == 0 ) return 0; int digit = value % radix; int sum = sum_digits ( value / radix, radix, it ) + digit; if ( it != 0 ) *it++ = digit; return sum; } int main() { std::cout<<"Enter an integer: "; int value; if ( std::cin>> value ) { int sum = sum_digits ( value, 10, &std::ostream_iterator<int> ( std::cout, " " ) ); std::cout<<"\nThe sum is "<< sum <<'\n'; } }
New members chased away this month: 4
![]() |
Similar Threads
- LC3 Calculator (Assembly)
Other Threads in the C++ Forum
- Previous Thread: Class Month Problem
- Next Thread: Makefile
Views: 2392 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets







