Codeforcoding

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

C program to display even and odd number in given range
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 find 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

C program to find whether a number is even or odd

 

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

C++ program to separate Odd and Even numbers from an array

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

C++ program calculate Average of odd and even numbers in an array 

C++ program to calculate the sum of odd and even numbers

C++ program to find whether a number is even or odd

 

Java program to find a number is even or odd using the method

Java program to separate Odd and Even numbers from an array

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

Java program display odd and even numbers without if statements

Java program to calculate the sum of odd and even numbers

Java program to find whether a number is even or odd

 

Java program to calculate sum of odd and even numbers

C++ program to calculate sum of odd and even numbers

Python program to calculate sum of odd and even numbers

 

Python code to display even and odd number from 1 to n
Java program to display even and odd number in the given range
Exit mobile version