QuestionAsk Questions?

Even and odd functions in java

View Answers

May 17, 2012 at 5:56 PM

Example:

import java.util.*; public class EvenOddFunction { public static void main(String[] args) { Scanner input = new Scanner(System.in); int number; System.out.println("Input any number to check even or odd:"); number = input.nextInt(); evenMethod( number); oddMethod(number); } public static void evenMethod(int n) { if (n % 2 == 0){ System.out.println("Your number " + n + " is even"); } } public static void oddMethod(int n) { if (n % 2 != 0) { System.out.println("Your number " + n + " is odd"); } } }

**Description :** Number which is divisible by 2 is called even numbers and which is not divisible by 2 is called odd numbers.
Here we have two separate functions for checking either number is even or odd. We are passing number as
argument in methods.

Related Tutorials/Questions & Answers: