- #1
needOfHelpCMath
- 72
- 0
Write a while loop that prints 1 to userNum, using the variable i. Follow each number (even the last one) by a space. Assume userNum is positive. Ex: userNum = 4 prints:
1 2 3 4
Run
✖ Testing with userNum = 4
Expected output: 1 2 3 4
Your output: 4 3 2 1
1 2 3 4
Code:
#include <iostream>
using namespace std;
int main() {
int userNum = 0;
int i = 0;
userNum = 4; // Assume positive
while ( userNum > i) {
cout << userNum << " ";
userNum = userNum - 1;
}
cout << endl;
return 0;
}
✖ Testing with userNum = 4
Expected output: 1 2 3 4
Your output: 4 3 2 1