Cpp program to display even and odd number in the given range
- Home
- Find elements
- Cpp program to display even and odd number in the given range
- On
- By
- 0 Comment
- Categories: 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
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
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