165k views
0 votes
In c++:

a. Print the integers from 1 to 20 using a while loop and the counter variable x.
b. Print only five integers per line.
[Hint: Use the calculation x % 5. When the value of this is 0, print a newline character, otherwise print a tab character.]

1 Answer

2 votes

Final answer:

To print the integers from 1 to 20 using a while loop and the counter variable x in C++, you can use the provided code.

Step-by-step explanation:

To print the integers from 1 to 20 using a while loop and the counter variable x in C++, you can use the following code:

#include <iostream>

int main() {
int x = 1;
while (x <= 20) {
std::cout << x << " ";
if (x % 5 == 0)
std::cout << "\\";
else
std::cout << "\t";
x++;
}
return 0;
}

This code will print the integers from 1 to 20, with five integers per line, separated by tabs. After printing five integers, it will move to the next line.

User NSNolan
by
7.3k points