Find elements

C program to display even and odd number in given range

C program to display even and odd number in the given range

In this tutorial, we discuss a concept of C program to display even and odd number in the given range

 

What is odd or even

when you devide a number by two and if the balance is zero, it is an even number

when you  divided number by two  and if the balance is one, it is an odd number

Display even and odd number in given range

Example of even number 2,4,6,8,…..

Example of odd number 1,3,5,7,…..

 

Here we will use a modular operator to find and display odd or even numbers in the given range.

Display even and odd number in given range using for loop

This program allows the user to enter two different digits and then, the program will display odd numbers and even numbers between entered digits using for loop

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1, num2,r,i;

    printf("Enter the first number for the range: ");
    scanf("%d",&num1); //received the number for num1
    printf("Enter the second number for the range: ");
    scanf("%d",&num2); //received the number for num2
    printf("\nDisplay the even numbers between %d and %d are: ",num1,num2);

    for(i=num1; i<=num2; i++){
        r=i%2;
        if(r==0)
            printf("\n %d",i);
    }

    printf("\n\nDisplay the odd numbers between %d and %d are: ",num1,num2);

    for(i=num1; i<=num2; i++){
        r=i%2;
        if(r==1)
            printf("\n %d",i);
    }
    getch();
    return 0;
}

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

Enter the first number for the range: 20
Enter the second number for the range: 40

Display the even numbers between 20 and 40 are:
20
22
24
26
28
30
32
34
36
38
40

Display the odd numbers between 20 and 40 are:
21
23
25
27
29
31
33
35
37
39

 

Display even and odd number in given range using while loop

Program 2

This program allows the user to enter two different digits and then, the program will display odd numbers and even numbers between entered digits using while loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,r,i;
    printf("Enter the first number for the range: ");
    scanf("%d",&num1);  //read first number to num1
    printf("Enter the second number for the range: ");
    scanf("%d",&num2);  //read second number to num2

    printf("\nDisplay even number between %d and %d are:",num1,num2);

    i=num1;
while(i<=num2){
r=i%2;
if(r==0)
printf("\n%d",i);
 i++;
}

printf("\n\nDisplay odd number between %d and %d are",num1,num2);

i=num1;
while(i<=num2){
r=i%2;
if(r==1)
printf("\n%d",i);
 i++;
}
getch();
    return 0;
}

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

Enter the first number for the range: 23
Enter the second number for the range: 35

Display even number between 23 and 35 are
24
26
28
30
34

Display odd number between 23 and 35 are
23
25
27
29
31
33
35

Display even and odd number in given range using do-while loop

Program 3

This program allows the user to enter two different digits and then, the program will display odd numbers and even numbers between entered digits using do-while loop

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,r,i;
    printf("Enter the first number for the range: ");
    scanf("%d",&num1);  //read first number to num1
    printf("Enter the second number for the range: ");
    scanf("%d",&num2);  //read second number to num2

    printf("\nDisplay even number between %d and %d are",num1,num2);

    i=num1;
do{
r=i%2;
if(r==0)
printf("\n%d",i);
 i++;
}while(i<=num2);

printf("\n\nDisplay odd number between %d and %d are",num1,num2);

i=num1;
do{
r=i%2;
if(r==1)
printf("\n%d",i);
 i++;
}while(i<=num2);
getch();
    return 0;
}

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

Enter the first number for the range: 45
Enter the second number for the range: 60

Display even number between 45 and 60 are:
46
48
50
52
54
56
58
60

Display odd number between 45 and 60 are:
45
47
49
51
53
55
57
59

 

 

 

Suggested for you

for loop in C language

While loop in C language

Do-while loop in C language

if statements in C language

 

Similar post

C program to check whether a number is even or odd

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

C program to separate Odd and Even numbers from an array

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

C program print odd and even numbers without if statements

Python code to display even and odd number from 1 to n
Java program to display even and odd number in the given range
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…

1 month ago

PHP Full Pyramid pattern program

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

1 month 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…

9 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,…

9 months ago