Java program to display even and odd number in the given range
- Home
- Find elements
- Java program to display even and odd number in the given range
- On
- By
- 0 Comment
- Categories: Find elements
Java program to display even and odd number in the given range
Java program to display even and odd number in the given range
In this tutorial, we discuss a concept of Java program to display even and odd number in the given range
What is odd or even?
When you divide a number by two and if the balance is zero, it is an even number
when the number divided 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
Program 1
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
import java.util.Scanner; class OddEvenRange{ public static void main (String args[]){ int r,i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number for the range: "); int num1=scan.nextInt();//reads num1 from user System.out.print("Enter the second number for the range: "); int num2=scan.nextInt();//reads num2 from user System.out.print("\nDisplay the even numbers between "+num1+" and "+num2+" are :"); for(i=num1; i<=num2; i++){ r=i%2; if(r==0) System.out.println(i); } System.out.print("\nDisplay the odd numbers between "+num1+" and "+num2+" are :"); for(i=num1; i<=num2; i++){ r=i%2; if(r==1) System.out.println(i); } } }
When the above code is executed, it produces the following results
Enter the first number for the range: 25 Enter the second number for the range: 40 Display the even numbers between 25 and 40 are: 26 28 30 32 34 36 38 40 Display the odd numbers between 25 and 40 are: 27 29 31 33 35 37 39
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
import java.util.Scanner; class OddEvenRange1{ public static void main (String args[]){ int r,i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number for the range: "); int num1=scan.nextInt();//reads num1 from user System.out.print("Enter the second number for the range: "); int num2=scan.nextInt();//reads num2 from user System.out.print("\nDisplay the even numbers between "+num1+" and "+num2+" are :"); i=num1; while(i<=num2){ r=i%2; if(r==0) System.out.println(i); i++; } System.out.print("\nDisplay the odd numbers between "+num1+" and "+num2+" are :"); i=num1; while(i<=num2){ r=i%2; if(r==1) System.out.println(i); i++; } } }
When the above code is executed, it produces the following results
Enter the first number for the range: 32 Enter the second number for the range: 44 Display the even numbers between 32 and 44 are: 34 36 38 40 42 44 Display the odd numbers between 32 and 44 are: 35 37 39 41 43
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 a do-while loop
import java.util.Scanner; class OddEvenRange2{ public static void main (String args[]){ int r,i; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number for the range: "); int num1=scan.nextInt();//reads num1 from user System.out.print("Enter the second number for the range: "); int num2=scan.nextInt();//reads num2 from user System.out.print("\nDisplay the even numbers between "+num1+" and "+num2+" are :\n"); i=num1; do{ r=i%2; if(r==0) System.out.println(i); i++; }while(i<=num2); System.out.print("\nDisplay the odd numbers between "+num1+" and "+num2+" are :\n"); i=num1; do{ r=i%2; if(r==1) System.out.println(i); i++; }while(i<=num2); } }
When the above code is executed, it produces the following results
Enter the first number for the range: 21 Enter the second number for the range: 35 Display even number between 21 and 35 are: 22 24 26 28 30 32 34 Display an odd number between 21 and 35 are: 21 23 25 27 29 31 33 35
Suggested for you
Do-while loop in Java language
if statements in Java 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