Program to print even or odd numbers in a given range using recursion in C++.
Solution:
#include <iostream> using namespace std; void calculate(int start, int n) { if(start> n) return; cout<<start<<"\t"; calculate(start+2, n); } int main() { int n; cout<<"Enter a number "; cin>>n; cout<<"Even Numbers are : "; calculate(2,n); cout<<"\nOdd Numbers are : "; calculate(1,n); return 0; }
Output :
Enter a number 20 Even Numbers are : 2 4 6 8 10 12 14 16 18 20 Odd Numbers are : 1 3 5 7 9 11 13 15 17 19