C++ program to print all the natural numbers from 1 to N using recursion has been shown here.
Page content(s):
Additional content(s):
1. C++ Program & output to Print First N Natural Numbers using recursion
Code has been copied
/********************************************** alphabetacoder.com C++ program to print all natual numbers from 1 to n using recursion ***********************************************/ #include <iostream> using namespace std; // recursive function void printUptoN(int n) { //condition for calling if (n > 1) printUptoN(n - 1); cout << n << " "; } int main() { // declare variable int n; //take input of the number upto which // natural numbers will be printed cout << "Enter the upper limit= "; cin >> n; cout << "First " << n << " natural numbers are : "; // call the recursive function to print // the natural numbers printUptoN(n); return 0; }
Output
Case 1:
Enter the upper limit=4
First 4 natural numbers are : 1 2 3 4
Case 2:
Enter the upper limit=8
First 8 natural numbers are : 1 2 3 4 5 6 7 8