Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying the next number

Tags:

c++

xcode

I'm trying to write a program that multiplies an input number by 2, and then multiples that answer by 2 in a loop, however I can't get my program to multiply the second number, here's my code.

int main() {

    int number;

    cout << "Enter a number: ";
    cin >> number;

    while (true) {
        int multiply = number * 2;
        cout << "Answer: " << multiply << endl;
    }  
} 

How do I make this program multiply the number that was previously multiplied? Thanks in advance!

like image 245
BjC Avatar asked Dec 19 '22 04:12

BjC


1 Answers

Just re-use the same variable:

while (true) {
    number = number * 2; // The same !
    cout << "Answer: " << number << endl;
}

But don't expect the program to run correctly until the end of times: int variables have a maxmimum of (2^31 - 1), so it will be ok at max for 30 iterations.

like image 74
Aracthor Avatar answered Dec 21 '22 18:12

Aracthor