0
Sponsored Links


Ad by Google
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();
 }
}

Sponsored Links

0 comments:

Post a Comment