Natural Numbers in C++ Program


Numbers that are greater than 0 are called natural numbers. The natural number are

1, 2, 3, 4, 5, 6, 7...

Algorithm

  • Initialise the number n.
  • Write a loop that iterates from 1 to n.
    • Print the numbers.
    • Increment the iterative variable.

Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h>
using namespace std;
void printNaturalNumbers(int n) {
   for (int i = 1; i <= n; i++) {
      cout << i;
   }
   cout << endl;
}
int main() {
   int n = 10;
   printNaturalNumbers(n);
   return 0;
}

Output

If you run the above code, then you will get the following result.

1 2 3 4 5 6 7 8 9 10

Updated on: 23-Oct-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements