Java Programming Examples Tutorial Index

Java Number Programs

This Java program is used to find whether values inserted within an array are odd even.



Example:

import java.util.*;

class pn {
 public static void main(String args[]) {
  Scanner sc = new Scanner(System.in);

  System.out.println("enter the value of size");
  int a = sc.nextInt();

  int arr[] = new int[a];

  for (int i = 0; < a; i++) {
   arr[i] = sc.nextInt();
  }
  for (int i = 0; < a; i++) {
   if (arr[i] % 2 == 0)
    System.out.println("even");
   else
    System.out.println("odd");
  }
 }
}

Explanation:

First you have to import the util package of Java so that you can use the Scanner class in this program which will help programmers to fetch input from users. Then define a class name 'pn'. Inside the class define the main() function. All Java program needs one main() function from where it starts executing program. Next define an object name 'sc' of Scanner class. Then System.out.println(); will be used to display a message - " enter the value of size". The size of array will be of type integer and will get stored in variable a using the object 'sc' of Scanner class.
Next, you have to declare an integer type array name arr[]. Now, implement for loop to insert values into the array. The for loop will start from 0 and goes one less, up to the value assigned to 'a'. Then you have to implement another for loop which will also iterate from 0 till a-1. Inside the array will be the if condition which will check whether i%2 is equal to zero or not, which means value of 'arr[i]' if divided by 2 remains no remainder then the number is even and so System.out.println() will display the message "even"; otherwise prints the message "odd".



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram