C++ Program - Print Numbers 1 to 10



Print Numbers 1 to 10

Here is a simple C++ program that will print the numbers 1 to 10:



Explanation of each line:


#include <iostream> 

This line includes the iostream library, which allows for input and output operations, such as printing to the console.


int main() 

This line defines the main function of the program, which is where the program's execution begins.


for (int i = 1; i <= 10; i++) { 

This line starts a for loop that initializes a variable i to 1, checks if i is less than or equal to 10, and increments i by 1 after each iteration. The loop will execute the code inside the curly braces 10 times.


std::cout << i << " "; 

This line prints the current value of i to the console, followed by a space. std::cout is the standard output stream and << is the insertion operator that sends the value of i to the standard output.


This curly brace marks the end of the for loop.


return 0; 

This line is the exit status of the program. If the program execution completes successfully, it will return 0.


The next curly brace marks the end of the program.


Console Output

And this will output the numbers from 1 to 10 in the console.




How to Print Each Number on a New Line

Note that the numbers are not separated by any line breaks, so they will all be on the same line in the console. If you want to separate the numbers by new lines you can use std::endl instead of " " after each number.



This will produce the following output:





Image by 200 Degrees from Pixabay

Comments

Popular posts from this blog

The New ChatGPT Reason Feature: What It Is and Why You Should Use It

Raspberry Pi Connect vs. RealVNC: A Comprehensive Comparison

The Reasoning Chain in DeepSeek R1: A Glimpse into AI’s Thought Process