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
Java code to check whether a number is even or odd
Java code to check a number is even or odd using method
Java code to display all even and odd number from 1 to n
Java program to separate even and odd number from an array
Java code to display even and odd numbers without if statements