C++ Exercises: Find the first 10 natural numbers
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?
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics