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:
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:
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?
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/cpp-exercises/for-loop/cpp-for-loop-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics