Find elements

Program to print even or odd numbers in given range using recursion

Program to print even or odd numbers in a given range using recursion in C language

In this tutorial, we will discuss a concept of the  C program to print odd or even number in given range using recursion

In this article, we are going to learn  how to  print odd and even numbers using recursion in the C programming language

 

What is an even or odd number?

When any integer ends in 0,2,4,6,8 and it can be divided by two with the remainder of zero, it is called as an even number.

Example of even numbers – 34,-64,78,788

When any integer ends in 0,1,3,5,7,9 and it cannot be divided without a remainder, it is called as an odd number.

Example for odd numbers – 33,-69,75,785

 

Here, We can use C programming operators to find odd or even number in the given range

 

C program to find odd or even number using recursion

Program 1

This program allows the user to enter two input(number) for the lower limit and upper limit. and  it finds whether odd and even numbers in the given range(between upper limit to lower limit)

#include <stdio.h>
#include <stdlib.h>
void displayEvenOdd(int num, int limit);
int main()
{
    int lowerLimit, upperLimit;
    printf("Enter lower limit\n");
    scanf("%d",&lowerLimit);  
//Received from user number for lower limit
    printf("Enter upper limit\n");
    scanf("%d",&upperLimit);
//Received from user number for upper limitr
    printf("Even/odd numbers from %d to %d are: ",lowerLimit,upperLimit);
    displayEvenOdd(lowerLimit,upperLimit);
    getch();
    return 0;
}

void displayEvenOdd(int num, int limit)//recursive function
{
    if(num>limit)
        return;
    printf("%d, ",num);

    displayEvenOdd(num + 2, limit);
}

When the above code is executed, it produces the following results

case 1

Enter lower limit
10
Enter upper limit
30
Even/odd numbers from 10 to 30 are: 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30

 

case 2

Enter lower limit
5
Enter upper limit
25
Even/odd numbers from 5 to 25 are: 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25

 

Similar post

C program to check whether a number is even or odd

C program to check a number is even or odd using the function

C program to separate Odd and Even numbers from an array

C program to Electricity bill calculation using the function

C program to display all even and odd numbers from 1 to n

C program display odd and even numbers without if statements

C program to calculate the sum of odd and even numbers

 

Suggested for you

Function in C programming language

Recursion in C programming language

If statements in C programming  language

The operator in C programming language

 

C++ program to print even or odd in given range using recursion
Use of Java program to find sum of two numbers using recursion
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago