55.3k views
2 votes
Write a program to print number from 1 to 10.
(int i ; while (i<=10) cout<

User Enricoza
by
8.1k points

1 Answer

3 votes

Final answer:

A corrected version of the C++ program to print numbers from 1 to 10 is provided, demonstrating the use of a while loop, proper variable initialization, and the cout statement for output.

Step-by-step explanation:

The question is asking for a simple program that will print numbers from 1 to 10. The code snippet provided contains some syntax errors which, when fixed, will allow the program to run as intended. Here is the corrected version of the program using C++ which includes initialization of the variable i, and correct syntax for the while loop and printing:

#include
using namespace std;

int main() {
int i = 1; // Initialize i to 1
while (i <= 10) {
cout << i << endl; // Print the current value of i
i++; // Increment i by 1
}
return 0;
}

This program uses a while loop to print numbers from 1 to 10. The variable i is initialized to 1, and on each iteration of the loop, it is increased by 1 using the increment operator (i++). The loop continues until i is greater than 10, at which point it stops. The cout statement is used to output each number followed by a newline character, represented by endl.

User Marcos Brigante
by
7.5k points