Natural Numbers in C++ Program

C++Server Side ProgrammingProgramming

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
raja
Published on 29-Jun-2021 09:59:00

Advertisements