Find elements

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

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

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

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

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

Dsplay even and odd number in the 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 display odd or even number in the given range.

if n%2==0,  n is a even number

if n%2==1,  n is a odd number

 

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 <iostream>
#include <conio.h>
using namespace std;

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

    cout<<"Enter the first number for the range: ";
    cin>>num1; //received the number for num1
    cout<<"Enter the second number for the range: ";
    cin>>num2; //received the number for num2
      cout<<"\n\nDisplay the even numbers between"<<num1 <<"and"<<num2<< "are";

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

     cout<<"\n\nDisplay the odd numbers between"<<num1 <<"and"<<num2<< "are";

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

 

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

Enter the first number for the range: 14
Enter the second number for the range: 25

Display the even numbers between 14 and 25 are
14
16
18
20
22
24

Display the odd numbers between 14 and 25 are
15
17
19
21
23
25

 

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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2,r,i;
    cout<<"Enter the first number for the range: ";
    cin>>num1;  //read first number to num1
    cout<<"Enter the second number for the range: ";
    cin>>num2;  //read second number to num2

    cout<<"\n\nDisplay even numbers between "<<num1<<" and "<<num2<<" are:";

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

cout<<"\n\nDisplay odd numbers between "<<num1<<" and "<<num2<<" are:";

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

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

Enter the first number for the range: 12
Enter the second number for the range: 25

Display even numbers betwen 12 and 25 are:
12
14
16
18
20
22
24

Display odd numbers betwen 12 and 25 are:
13
15
17
19
21
23
25

 

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 <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int num1,num2,r,i;
    cout<<"Enter the first number for the range: ";
    cin>>num1;  //read first number to num1
    cout<<"Enter the second number for the range: ";
    cin>>num2;  //read second number to num2

    cout<<"\n\nDisplay even numbers between "<<num1<<" and "<<num2<<" are:";

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

cout<<"\n\nDisplay odd numbers between "<<num1<<" and "<<num2<<" are:";

i=num1;
do{
r=i%2;
if(r==1)
cout<<"\n"<<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: 27
Enter the second number for the range: 35

Display even numbers between 27 and 35 are:
28
30
32
34

Display odd numbers between 27 and 35 are:
27
29
31
33
35

 

 

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

Cpp program to check whether a number is even or odd

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

Cpp program to separate even and odd number from an array

Cpp program to display even and odd numbers from 1 to n

Display odd and even numbers without if statements

 

 

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

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