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

 

How do we identify whether a given number is odd or even?

when you divide 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 numbers. After the program displays odd numbers and even numbers from entered digits using a 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

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

 

Python program check whether a number is odd or even

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

Python program to display even and odd numbers without if

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

Separate odd and even numbers in a list to different two list

Python Program to find odd and even numbers from a list

 

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

 

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

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

2 days ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago