Print Numbers from 1 to 100 Using a For Loop in C++

In this exercise, you will learn how to use a `for` loop in C++ to print numbers from 1 to 100. A `for` loop is commonly used when you know the number of iterations in advance. In this case, we want to iterate exactly 100 times, printing the numbers in sequence.

The `for` loop structure consists of three parts:
1. Initialization: The starting value of the counter.
2. Condition: The loop will continue as long as the condition is true.
3. Increment: The counter will be increased or decreased after each iteration.

In this program, you will initialize a counter variable to 1 and continue the loop until the counter reaches 100. Each iteration will print the current value of the counter.

This exercise will help you understand how to control the flow of your program using loops in C++.

Group

Flow Control in C++

Objective

1. Use a for loop to iterate from 1 to 100.
2. In each iteration, print the current number.
3. Test the program to make sure it prints all numbers from 1 to 100.

Print numbers from 1 to 100 using a for loop.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library for input and output

using namespace std; // Use the standard namespace

// Main function - the entry point of the program
int main() {
    // Use a for loop to iterate from 1 to 100
    for (int i = 1; i <= 100; ++i) {
        cout << i << endl; // Print the current value of i
    }

    return 0; // Return 0 to indicate that the program executed successfully
}

 Output

1
2
3
4
5
6
7
8
9
10
...
99
100

Share this C++ Exercise


More C++ Programming Exercises of Flow Control in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.