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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With