| | |
How do I convert an integer into digits and add their sum?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 0
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:
Thank you for the help
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cmath> using namespace std; int main() { int num, num2, sum = 0, dig; cout << "Please enter a number" << endl; cin >> num; if(num < 0) num = -num; num2 = num; dig = num; do { do { dig = dig / 10; } while (dig >= 10); cout << dig << " "; if (num > 100) num = num - (100*(num/100)); else num = num - (10*(num/10)); sum = sum + num2 % 10; num2 = num2 / 10; dig = num; } while (num2 > 0); cout << endl; cout << "The sum of the digits is " << sum << endl; return 0; }
Thank you for the help
Last edited by ShadowOfBlood; Sep 29th, 2008 at 2:03 pm.
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:
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
C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { std::cout<<"Enter a number: "; int num; if ( std::cin>> num ) { int sum = 0; if ( num < 0 ) num = -num; while ( num != 0 ) { sum += num % 10; num /= 10; } std::cout<<"The sum is "<< sum <<'\n'; } }
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.
•
•
•
•
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.
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 0
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).
![]() |
Similar Threads
- LC3 Calculator (Assembly)
Other Threads in the C++ Forum
- Previous Thread: Class Month Problem
- Next Thread: Makefile
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






