1

My problem is that I'm having to take a 5-digit integer input given by the user, and separate the integer into its individual digits. I then need to print those digits in reverse order and also print out the sum of those digits. Here is what I have coded so far to separate the 5-digit integer into its individual digits. Please note that I am limited to using integer division and modulo operators for separating the integer into its digits.

#include <iostream>

using namespace std;
int main() {
int number;

    cout << "Enter a five-digit number: ";
    cin >> number;

    cout << number / 10000 << " ";
    number = number % 10000;
    cout << number / 1000 << " ";
    number = number % 1000;
    cout << number / 100 << " ";
    number = number % 100;
    cout << number / 10 << " ";
    number = number % 10;
    cout << number << endl;

    return 0;
}

For example, when the user inputs a 5-digit number like 77602, the program should output it as

7 7 6 0 2 and the sum of the digits is 22.

How do I go about printing this in reverse order as well as the sum of the individual digits?

Edit: Few spelling and grammatical errors.

Ken White
  • 123,280
  • 14
  • 225
  • 444
Ben Rogers
  • 45
  • 6
  • 3
    By using an array to store the individual digits. You can then process them to your hearts content. Print in reverse or sideways. Sum or multiply. The world's your oyster when you've got arrays. – StoryTeller - Unslander Monica Dec 04 '16 at 21:38
  • [Convert the integer to a string](http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c), then iterate it reversely, display each and apply your summation (But watch out that std::string has the numbers as characters, when your summations needs them by value). – Khalil Khalaf Dec 04 '16 at 21:40
  • 1
    StoryTeller's comment definitely goes to the most general solution here. For the specific problems you're trying to solve (element-wise operations like summing and LIFO operations like reversing), you can also use a more restricted data structure -- a stack. This won't particularly simplify things if used directly (use an array instead), but does indicate that there's a graceful recursive solution to this problem if you prefer that style. – addaon Dec 04 '16 at 21:41
  • If the user inputs 77602, it seems like printing in reverse order would call for 2 0 6 7 7, wouldn't it? If so, it's really simple: just take the remainder when dividing by 10; that's the current digit. Write it out, and add it to the running sum. Then divide the number by 10. Repeat until done. – Pete Becker Dec 04 '16 at 21:43

4 Answers4

2

It's a lot easier to reverse a string than an integer. It's also a lot easier to accumulate individual digits of a string containing a number than an integer. Try this:

#include <iostream>
#include <string>
#include <algorithm>
int main(int argc, char *argv[])
{
    std::string number;
    int accum = 0;
    std::cout << "Enter a five digit number: ";
    std::cin >> number;

    if (number.length() != 5)
    {

        std::cout << std::endl << "I asked for five digits!" << std::endl;
        return 0;
    }

    for (int i = 0; i < 5; i++)
    {
        if (number.at(i) < '0' || number.at(i) > '9')
        {
            std::cout << std::endl << "Non-integer string entered" << std::endl;
            return 0;
        }

        accum += (number.at(i) - '0');

    }

    std::reverse(number.begin(), number.end());
    std::cout << "Reversed: " << number << std::endl << "Sum of digits: " << accum << std::endl;



    return 0;
}
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • I tried copying this into Eclipse CDT, and it gives an error saying that 'reverse' is not a member of 'std' and fails to build the project due to that error. Any fix? – Ben Rogers Dec 04 '16 at 21:50
  • @GovindParmar That solved the issue for that error, however now the cout statement "I asked for five digits!" is printed despite me also trying to take std: :cout << "Enter a five digit number: "; and std: :cin >> number; out of the loop – Ben Rogers Dec 04 '16 at 21:56
1

A simple solution using the same logic and tools of your code..

    int sum = 0;
    for(int i = 0; i < 5; ++i)
    {
        int digit = number % 10;
        sum += digit;
        cout << digit << " ";
        number /= 10;
    }

    cout << "\nSum of digits: " << sum << "\n";
A.S.H
  • 29,101
  • 5
  • 23
  • 50
  • Two comments. First, use `std::cout, not `cout`. Second, define `digit` inside the loop, not outside; in general, give variables the smallest necessary scope. Since `digit` has no meaningful value after the loop, there's no need to define it outside the loop. – Pete Becker Dec 04 '16 at 22:01
  • @PeteBecker agree for the scope (edited, although I was nto substitute a portion of code where OP was `using namespace std`, so as in the title, and unlike the other answers, I was trying to be as close as possible to the OP's ground. – A.S.H Dec 04 '16 at 22:05
  • Understood. Of course, `using namespace std;` is abomination, and should be struck from wherever it appears. – Pete Becker Dec 04 '16 at 22:08
1

This is probably easiest solution for you to understand:

 #include <iostream>

using namespace std;
int main()
{
int number;
int number2;
int numberReverse;
int sum = 0;

cout << "Enter a five-digit number: ";
cin >> number;

numberReverse = number;

cout << number / 10000 << " ";
sum = sum + number/10000;
number = number % 10000;
cout << number / 1000 << " ";
sum = sum + number/1000;
number = number % 1000;
cout << number / 100 << " ";
sum = sum + number/100;
number = number % 100;
cout << number / 10 << " ";
sum = sum+number/10;
number = number % 10;
cout << number << endl;
sum = sum+number;

cout << "Reverse: " << endl;

number2 = numberReverse%10;
cout << number2 << " ";
number2 = (numberReverse/10)%10;
cout << number2 << " ";
number2 = (numberReverse/100)%10;
cout << number2 << " ";
number2 = (numberReverse/1000)%10;
cout << number2  << " ";
number2 = (numberReverse/10000)%10;
cout << number2 << endl;

cout << "Sum is " << sum;

return 0;
}
  • Please annotate or clarify how this solution differs from the OP's posting. – Thomas Matthews Dec 04 '16 at 21:58
  • This solves the problem of finding and printing the sum of the individual digits, however it does not solve the problem of printing them in reverse order. Thanks for the assistance though! – Ben Rogers Dec 04 '16 at 22:01
  • He didn't know how to get sum of digits, judging by that he is probably very new to programming so I wrote this code using only fundamental stuff so he can undestand it - If he dosen't know this then I think he dosen't know for FOR() either. – Nenad Markovic Dec 04 '16 at 22:03
  • I will edit code so it print digits reverse for a minute :) – Nenad Markovic Dec 04 '16 at 22:04
  • Now it print reverse, if you don't understand something please aks :) – Nenad Markovic Dec 04 '16 at 22:16
  • That works perfectly, though I had to make a few edits to fit the proper output format that was requested of me. I understand now that I somewhat had the right idea of how to get the sum and reverse before originally posting the question, just wasn't sure of how to implement it. Anyways, thank you very much. – Ben Rogers Dec 04 '16 at 22:24
  • Glad to help :) – Nenad Markovic Dec 04 '16 at 22:29
0

I think, you need to do something like this:

int number;

... some IO operations ...

std::vector<int> digits;
do {
    digits.push_back(number % 10);
    number /= 10;
} while (number);

After this all you need to do is 2-3 loops through vector digits

for (std::size_t k = digits.size() - 1; k >= 0; --k)
   std::cout << digits[k] << " ";
std::cout << std::endl;
for (std::size_t k = 0; k < digits.size(); ++k)
    std::cout << digits[k] << " ";
std::cout << std::endl;
int sum = 0;
for (std::size_t k = 0; k < digits.size(); ++k)
    sum += digits[k];
std::cout << sum;
Semyon Burov
  • 1,090
  • 1
  • 9
  • 15