w3resource

C++ Exercises: Find the first 10 natural numbers

C++ For Loop: Exercise-1 with Solution

Write a program in C++ to find the first 10 natural numbers.

Visual Presentation:

C++ Exercises: Find the first 10 natural numbers

Sample Solution :-

C++ Code :

#include <iostream> // Preprocessor directive to include the input/output stream header file

using namespace std; // Using the standard namespace to avoid writing std::

int main() // Start of the main function
{
    int i; // Declare an integer variable 'i'

    cout << "\n\n Find the first 10 natural numbers:\n"; // Display a message to find the first 10 natural numbers
    cout << "---------------------------------------\n"; // Display a separator line
    cout << " The natural numbers are: \n"; // Display a message indicating the list of natural numbers

    // Loop to print the first 10 natural numbers
    for (i = 1; i <= 10; i++) 
    {
        cout << i << " "; // Output each natural number followed by a space
    }
    cout << endl; // Move to the next line after printing the numbers
}

Sample Output:

 Find the first 10 natural numbers:                                    
---------------------------------------                                
 The natural numbers are:                                              
1 2 3 4 5 6 7 8 9 10  

Flowchart:

Flowchart: Find the first 10 natural numbers

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous: C++ For Loop Exercises Home
Next: Write a program in C++ to find the sum of first 10 natural numbers.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.