Write a while loop that prints 1 to userNum using the variab
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:
#include <iostream>
using namespace std;
int main() {
int userNum = 0;
int i = 0;
userNum = 4; // Assume positive
/* Your solution goes here */
cout << endl;
return 0;
}
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
using namespace std;
int main() { // driver method
int i = 0; // required initialisations
int userNum = 0;
userNum = 4; //local variable to get the loop itearated
while(i < userNum) { // looping till the desired output gets printed
++i; // incrementing the value
cout << i << \" \"; // printing to the console
}
cout << endl; // new line character code
return 0;
}
OUTPUT :
1 2 3 4
Hope this is helpful.
