Java program to write odd and even numbers into two files

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

Java program to write odd and even numbers into two files
Display an 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

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

for loop in java language

while loop in Java language

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

JavaObject Oriented ProgrammingProgramming

To split the Even and Odd elements into two different lists, the Java code is as follows −

Example

 Live Demo

import java.util.Scanner; public class Demo{    public static void main(String[] args){       int n, j = 0, k = 0;       Scanner s = new Scanner(System.in);       System.out.println("Enter the number of elements required :");       n = s.nextInt();       int my_arr[] = new int[n];       int odd_vals[] = new int[n];       int even_vals[] = new int[n];       System.out.println("Enter the elements of the array(even and add numbers) :");       for(int i = 0; i < n; i++){          my_arr[i] = s.nextInt();       }       for(int i = 0; i < n; i++){          if(my_arr[i] % 2 != 0){             odd_vals[j] = my_arr[i];             j++;          } else {             even_vals[k] = my_arr[i];             k++;          }       }       System.out.print("The odd numbers in the array : ");       if(j > 1){          for(int i = 0;i < (j-1); i++){             if(odd_vals[i]==1){                System.out.println("1 is niether even nor odd");             }             else             System.out.print(odd_vals[i]+",");          }          System.out.print(odd_vals[j-1]);       } else {          System.out.println("There are no odd numbers.");       }       System.out.println("");       System.out.print("The even numbers in the array : ");       if(k > 1){          for(int i = 0; i < (k-1); i++){             if(even_vals[i]==1){                System.out.println("1 is niether even nor odd");             }             else             System.out.print(even_vals[i]+",");          }          System.out.print(even_vals[k-1]);       } else {          System.out.println("There are no even numbers in the array.");       }    } }

Output

Enter the number of elements required : Enter the elements of the array(even and add numbers) : The odd numbers in the array : 1 is niether even nor odd 9 The even numbers in the array : 2,4,6

Console input

5 1 2 4 6 9

A class named ‘Demo’ contains the main function that asks for the number of elements that should be stored in the array and declares two new arrays that will store the odd values and even values respectively. The array elements are taken from the user and a ‘for’ loop is run to check if the number is divisible by 0, i.e checking if the remainder when the number is divided by 2 is 0. If yes, then that number from the main array will be stored in the even array, and in the odd array otherwise. Since 1 is neither even nor odd, it prints the message while storing 1 in the even or odd array.

Java program to write odd and even numbers into two files

Updated on 07-Jul-2020 09:16:21

import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter a number: "); int num = reader.nextInt(); if(num % 2 == 0) System.out.println(num + " is even"); else System.out.println(num + " is odd"); } }

Output

Enter a number: 12 12 is even

In the above program, a Scanner object, reader is created to read a number from the user's keyboard. The entered number is then stored in a variable num.

Now, to check whether num is even or odd, we calculate its remainder using % operator and check if it is divisible by 2 or not.

For this, we use if...else statement in Java. If num is divisible by 2, we print num is even. Else, we print num is odd.

We can also check if num is even or odd by using ternary operator in Java.

Example 2: Check whether a number is even or odd using ternary operator

import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter a number: "); int num = reader.nextInt(); String evenOdd = (num % 2 == 0) ? "even" : "odd"; System.out.println(num + " is " + evenOdd); } }

Output

Enter a number: 13 13 is odd

In the above program, we've replaced if...else statement with ternary operator (? :).

Here, if num is divisible by 2, "even" is returned. Else, "odd" is returned. The returned value is saved in a string variable evenOdd.

Then, the result is printed on the screen using string concatenation.