I\'m trying to learn c++ on my own and I\'ve hit a bit of a road block. The problem is I need to take an integer,split it into its digits and get the sum of the digits and
I used this for input for 5 numbers
int main () {
using namespace std;
int number
;`
cout << "Enter your digit: ";
cin >> number;
if (number == 0)
return 0;
int a = number % 10;
int second = (number / 10);
int b = second % 10;
int third = (second / 10);
int c = third % 10;
int fourth = (third / 10);
int d = fourth % 10;
int fifth = (fourth / 10);
int e = fifth % 10;
cout << e << " " << d << " " << c << " " << b << " " << a << endl;
return 0;
}
Push the digits onto a stack.
After you've gotten all the digits,
sum = 0 ;
while( stack not empty ) {
pop the stack to get a digit
sum += digit
display digit
}
display sum
You need a stack, you say? Use the STL's stack: std::stack<int>
Your problem comes from the fact that you are reading the digits backwards, thus you need to print them out backwards. A stack will help you tremendously.
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <stack>
int countDigitsInInteger(int n)
{
int count =0;
while(n>0)
{
count++;
n=n/10;
}
return count;
}
using namespace std;
int main(int argc, char *argv[])
{
int intLength =0;
int number;
int digit;
int sum = 0;
string s;
cout << "Please enter an integer ";
cin >>number;
cout << "Orginal Number = "<<number <<endl;
//make the number positive
if (number<0)
number = -number;
intLength = countDigitsInInteger(number);
//break apart the integer into digits
stack<int> digitstack;
while(number>0)
{
digit = number % 10;
number = number / 10;
digitstack.push(digit);
sum = sum+digit;
}
while(digitstack.size() > 0)
{
cout << digitstack.top() << " ";
digitstack.pop();
}
cout <<endl <<"Sum of the digits is: "<<sum<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Oh, and BTW, keep your indentation clean. Its important.
EDIT: In response to Steve Townsend, this method is not necessarily overkill, it is just different from yours. The code can be slimmed down so that it seems less like overkill:
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int getInput(string prompt)
{
int val;
cout << prompt;
cin >> val;
return val < 0 ? -val : val;
}
int main(int argc, char** argv)
{
int num = getInput("Enter a number: ");
cout << "Original Number: " << num << endl;
stack<int> digits;
int sum = 0;
while(num > 0)
{
digits.push(num % 10);
sum += digits.top();
num = num / 10;
}
while(digits.size() > 0)
{
cout << digits.top() << " ";
digits.pop();
}
cout << endl << "Sum of digits is " << sum << endl;
return 0;
}
Use std::stack to store separate digits, then print out the contents of stack.
here is a nice article: http://en.wikipedia.org/wiki/Stack_(data_structure)
here is how to use stacks in C++: http://www.sgi.com/tech/stl/stack.html
Let's not forget the stringstream approach, which I also find elegant.
#include <iostream>
#include <sstream>
int main()
{
int num = 123456789;
std::cout << "Number: " << num << std::endl;
std::stringstream tmp_stream;
tmp_stream << num;
std::cout << "As string: " << tmp_stream.str() << std::endl;
std::cout << "Total digits: " << tmp_stream.str().size() << std::endl;
int i;
for (i = 0; i < tmp_stream.str().size(); i++)
{
std::cout << "Digit [" << i << "] is: " << tmp_stream.str().at(i) << std::endl;
}
return 0;
}
Outputs:
Number: 123456789
As string: 123456789
Total digits: 9
Digit [0] is: 1
Digit [1] is: 2
Digit [2] is: 3
Digit [3] is: 4
Digit [4] is: 5
Digit [5] is: 6
Digit [6] is: 7
Digit [7] is: 8
Digit [8] is: 9
A simple solution:
int n = 12345;
vector<int> digits;
while (n != 0) {
digits.insert(digits.begin(), n%10);
n /= 10;
}
for(auto & i : digits)
cout << i << " ";
output: 1 2 3 4 5