Problem Statement: Print 1 to n numbers using recursion in cpp (c++).
Note:- Scroll horizontally to see the full line of code.
// Program to print 1 to n numbers using recursion
#include <iostream>
using namespace std;
void print(int n)
{
if (n == 0)
{
return;
}
print(n - 1);
cout << n << " ";
}
int main()
{
int n;
cout << "Enter n: ";
cin >> n;
print(n);
cout << endl;
return 0;
}
Comments
Post a Comment