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


Introduction

I have used Code::blocks 12 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

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


  1. #include <stdio.h>
  2.  
  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. printf("Enter lower limit: ");
  13. scanf("%d", &lowerLimit);
  14. printf("Enter upper limit: ");
  15. scanf("%d", &upperLimit);
  16.  
  17. printf("Even/odd Numbers from %d to %d are: ", lowerLimit, upperLimit);
  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. printf("%d, ", cur);
  32.  
  33. // Recursively call to printevenodd to get next value
  34. printevenodd(cur + 2, limit);
  35. }
  36.  

Result

C language techstudy.org

     





Related Topics




Popular Posts