Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split an integer and find the largest sum C++

Tags:

c++

I'm starting learning C++ on my own and I'm confused with one assignment which I'm trying to complete. User shoud type in natural numbers as long as he wants until he types in 0. After that my program should find the largest sum of digits which were typed and print it out. It shoud also print out a number from which it took the sum. Here is what I tried to do:

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    int input = 0;
    int digit;
    int sum = 0;
    int largest = 0;

    do
    {
        cout << "enter a natural number (0 if done): " << flush;
        cin >> input;

        while (input > 0)
        {
            digit = input % 10;
            sum = sum + digit;
            input = input / 10;
        }
        if (sum > largest)
            largest = sum;  

    } while (input);

    cout << "Max sum of digits was " << largest << "for" << endl;
}

When I'm running the programm it counts sum of digits from only first typed in number and stop working. When I take while (input > 0) away, it makes a loop, but doesn't count digits. I'll be very grateful for help and explanation. P.S. Sorry for my English, I'm not a native speaker.

like image 265
Anastasia Logvina Avatar asked Oct 13 '15 22:10

Anastasia Logvina


2 Answers

You seem to have three problems here:

1 - You are trying to use a variable that you essentially set to zero in your while loop

2 - You seem to be looking for the input that is for the largest sum

3 - You are not resetting your sum variable for each input

The solution for the first problem is to "backup" the input into another variable before modifying it and using that variable for the while loop.

That also allows for you to get what the largest number inputted is and store it.

int input = 0;
int inputBackup = 0;
int digit;
int sum = 0;
int largest = 0;
int largestInput = 0;

To add in the inputBackup variable, put it after the cin.

Then set the largestInput in your sum > largest if statement to set the largestInput if it is the largest.

cout << "enter a natural number (0 if done): " << flush;
cin >> input;

inputBackup = input;// This line
sum = 0;            // and this line

while (input > 0)
{
    digit = input % 10;
    sum = sum + digit;
    input = input / 10;
}
if (sum > largest)
{
    largest = sum;
    largestInput = inputBackup;// Store largest input
}

Then change while(input) to while(inputBackup) to check the inputBackup variable instead of the input one.

Change your cout to be like this to add the largestInput variable into it

cout << "Max sum of digits was " << largest << " for " << largestInput << endl;

And your code should be fixed!

Happy Coding!

like image 151
Elipzer Avatar answered Oct 31 '22 18:10

Elipzer


do
{
    cout << "enter a natural number (0 if done): " << flush;
    cin >> input;
    //more code
} while (input); 

To make this work correctly, input may not change between the cin and the loop condition.

But

while (input > 0) {
    digit = input % 10;
    sum = sum + digit;
    input = input / 10;
}

does change input.

Replace it with something like

int input2 = input;
while (input2 > 0) {
    digit = input2 % 10;
    sum = sum + digit;
    input2 = input2 / 10;
}
like image 22
deviantfan Avatar answered Oct 31 '22 19:10

deviantfan