Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.

#include <stdio.h>
 
 
// Function declaration
void printevenodd(int cur, int limit);
 
int main()
{
    int lowerLimit, upperLimit;
 
    // Inputting lower and upper limit from user
    printf("Enter lower limit: ");
    scanf("%d", &lowerLimit);
    printf("Enter upper limit: ");
    scanf("%d", &upperLimit);
 
    printf("Even/odd Numbers from %d to %d are: ", lowerLimit, upperLimit);
    printevenodd(lowerLimit, upperLimit);
 
    return 0;
}
 
 
//Recursive function to print even or odd numbers in a given range.
 
void printevenodd(int cur, int limit)
{
    if(cur > limit)
        return;
 
    printf("%d, ", cur);
 
    // Recursively call to printevenodd to get next value
    printevenodd(cur + 2, limit);
}

Result:

Enter lower limit: 10

Enter upper limit: 40

Even/odd Numbers from 10 to 40 are: 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40,

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C program to find reverse of a number using ... >>
<< Write C program to find sum of natural numbers in ...