print counting in c++
Here is an example of how you can print counting in C++:
#include <iostream>
int main() {
for (int i = 1; i <= 10; i++) {
std::cout << i << " ";
}
return 0;
}
Explanation:
The code begins with the
#include <iostream>
directive, which allows us to use the input/output stream objects in C++.The
main()
function is the entry point of the program. It is where the execution starts.Inside the
main()
function, we have afor
loop. Thefor
loop is used to iterate a specific number of times.The loop is initialized with
int i = 1
, which sets the initial value of the loop control variablei
to 1.The loop condition
i <= 10
specifies that the loop should continue as long as the value ofi
is less than or equal to 10.After each iteration of the loop, the
i
variable is incremented by 1 using thei++
statement.Inside the loop, we use
std::cout
to print the value ofi
followed by a space.std::cout
is the standard output stream object in C++, and the<<
operator is used to insert the value ofi
into the output stream.Finally, the
return 0;
statement is used to indicate that the program has executed successfully and is ready to exit.
When you run this program, it will output the numbers from 1 to 10, each separated by a space.