Write C++ program to print even or odd numbers in given range using recursion


I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // Function declaration
  5. void printevenodd(int cur, int limit);
  6.  
  7. int main()
  8. {
  9. int lowerLimit, upperLimit;
  10.  
  11. // Inputting lower and upper limit from user
  12. cout<<"Enter lower limit: ";
  13. cin>>lowerLimit;
  14. cout<<"Enter upper limit: ";
  15. cin>>upperLimit;
  16. //printf("Even/odd Numbers from %d to %d are: ", lowerLimit, upperLimit);
  17. cout<<"Even/odd Numbers from "<<lowerLimit <<" to "<<upperLimit<<endl;
  18. printevenodd(lowerLimit, upperLimit);
  19.  
  20. return 0;
  21. }
  22.  
  23.  
  24. //Recursive function to print even or odd numbers in a given range.
  25.  
  26. void printevenodd(int cur, int limit)
  27. {
  28. if(cur > limit)
  29. return;
  30.  
  31. cout<<cur<<endl;
  32.  
  33. // Recursively call to printevenodd to get next value
  34. printevenodd(cur + 2, limit);
  35. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts