Print first n Numbers?
#include <bits/stdc++.h>
using namespace std ;
void print(int n){
//Base case
if (n==0) return ; //Mandatory
//Recursive case
print(n-1); // 1 2 3 4 ...n-1
//Calculation
cout << n << '\n';
return ; //Optional
}
void print2(int n){
//Base case
if (n==0) return ; //Mandatory
cout << n << '\n';
//Recursive case
print2(n-1); ///n-1....3 2 1
return ; //Optional
}
int main(){
int n ;
cin >> n;
print2(n);
return 0 ;
}
Comments
Post a Comment