Java program to check even or odd number

Very basic Java coding practice or Java homework questions. While learning Java, we started with converting basic calculation to using Java program. like creating calculator using Java code, printing sum of digits and all. And sometimes this type of questions also asked in core Java written interview questions, although this question is not as demanded as how to balanced the parentheses using Java program.

How to check a number is even or odd?
package com.javamakeuse.poc;

import java.util.Scanner;

/**
 * 
 * @author javamakeuse
 *
 */
public class EvenOrOdd {
 public static void evenOrOdd() {
  System.out.println("Enter an Integer number:");

  try (Scanner in = new Scanner(System.in)) {
   int num = in.nextInt();
   // if input number is divisible by 2
   if (num % 2 == 0) {

    System.out.println("Entered number is even");
   } else {

    System.out.println("Entered number is odd");
   }
  }
 }

 public static void main(String[] args) {

  evenOrOdd();
 }
}

Comments

Popular posts from this blog

What are the core classes and interfaces of hibernate 4

JSP Servlet Interview Questions For Freshers Series 1

Java Banking Finance Services and Insurance (BFSI) domain interview questions