Number is even or odd in java

Java Program to Check Whether a Number is Even or Odd

In this article, we will write two java programs to check whether a number is even or odd. If a number is perfectly divisible by 2 ( number % 2 ==0 ) then the number is called even number, else the number is called odd number.

Perfectly divisible by 2 means that when the number is divided by 2 the remainder is zero. To check for remainder, we use modulus operator in java.

Example 1: Program to check if the number entered by user is even or odd

In this program, we have an int variable num . We are using Scanner class to get the number entered by the user.

Once the entered number is stored in the variable num, we are using if..else statement to check if the number is perfectly divisible by 2 or not. Based on the outcome, it is executing if block (if condition true) or else block (if condition is false).

import java.util.Scanner; public class JavaExample < public static void main(String args[]) < int num; System.out.print("Enter an Integer number: "); //The input provided by user is stored in num Scanner input = new Scanner(System.in); num = input.nextInt(); // If number is divisible by 2 then it's an even number //else it is an odd number if ( num % 2 == 0 ) System.out.println(num+" is an even number."); else System.out.println(num+" is an odd number."); >>

Output 1:
Java Program even odd output
Output 2:
Java Program even odd output2

Читайте также:  Java string поиск символа

Example 2: Program to check even odd number using ternary operator

A ternary operator is a one liner replacement of an if-else statement. In this program, we are using the same logic that we have seen above, however instead of using if..else, we are using a ternary operator here.

import java.util.Scanner; public class JavaExample < public static void main(String[] args) < Scanner scan = new Scanner(System.in); System.out.print("Enter any number: "); int num = scan.nextInt(); //checking if else using ternary operator //ternary operator syntax: condition ? expression1 : expression2 // if condition is true, expression1 executes else expression2 String evenOrOdd = (num % 2 == 0) ? "even number" : "odd number"; System.out.println(num + " is an " + evenOrOdd); >>

Output 1: When user enters 32 number.

Enter any number: 32 32 is an even number

Output 2: When user enters 13 number.

Enter any number: 13 13 is an odd number

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

Comments

if i run any programs like usergetinputs it shows the following error
” class java.util.Scanner not found in import.”
please help me to correct this error thank you.

Источник

Java code to check a number is even or odd using Method

 Java code to check a number is even or odd using Method

Java code to check a number is even or odd using Method

In this program, we will discuss the Java code to check a number is even or odd using Method

In this program, we are going to learn about how to find odd or even number from given number using the method in the Java language

What is Even or Odd

When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10

and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers

In my previous post, I have explained the various ways to check whether the number is even or odd in Java language

However, in this program, we embed the logic of the check even or odd number program in the method

you can embed the logic of the program to check even or odd numbers using any of the following approaches in the method

Once This program received the number it will check the given number either odd or even.

After receiving the input from the user, it is stored in the variable of num and then program divides the value of num by 2 and displays the output

Using modular operator

import java.util.Scanner; class Even_Odd1 < public static void main (String args[])< Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for check odd or even: "); int num=scan.nextInt(); //reads the value from the user find_Oddeven(num);//calling the method >//create a user defined metod static void find_Oddeven(int num) >

When the above code is executed, it produces the following results

Enter a number for check odd or even: 234 234 Is even
Enter a number for check odd or even: 253 253 is odd

Using modular operator with the return

import java.util.Scanner; class Even_Odd1 < public static void main (String args[])< Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the number for check odd or even: "); int num=scan.nextInt(); if(num%2==0) System.out.println(num+" is even"); else System.out.println(num+" is odd"); //reads the value from the user find_Oddeven(num);//calling the method >//create a user defined method with return static boolean find_Oddeven(int num) >

When the above code is executed, it produces the following results

Enter a number to check odd or even 344 344 is even
Enter a number to check odd or even 47 47 is odd

Using Bitwise operator

import java.util.Scanner; class OddEvenMethod5 < public static void main (String args[])< Scanner scan=new Scanner(System.in); System.out.print("Enter the number for check odd or even: "); int num=scan.nextInt();//reads the value from the user isEvenOrOdd(num); //call the method >//create a user define method to find odd or even static void isEvenOrOdd(int num) >

When the above code is executed, it produces the following results

Enter a number for check odd or even: 654 654 Is an even number
Enter a number for check odd or even: 323 323 is an odd number

Using ternary operator

import java.util.Scanner; class OddEvenMethod4 < public static void main (String args[])< Scanner scan=new Scanner(System.in); System.out.print("Enter the number for check odd or even: "); int num=scan.nextInt();//reads the value from the user isEvenOrOdd(num); //call the method >//create a user defined method to fine odd or even static void isEvenOrOdd(int num) >

When the above code is executed, it produces the following results

Enter a number for check odd or even: 567 567 Is odd
Enter a number for check odd or even: 124 124 is even

Using Division operator

import java.util.Scanner; class OddEvenMethod2 < public static void main (String args[])< Scanner scan=new Scanner(System.in); System.out.print("Enter the number for check odd or even: "); int num=scan.nextInt();//reads the value from the user isEvenOrOdd(num); //call the method >//create a user defined method to find odd even static void isEvenOrOdd(int num) >

When the above code is executed, it produces the following results

Enter the number for check odd or even: 501 501 is Odd
Enter the number for check odd or even: 200 200 is even

Using shift operator

import java.util.Scanner; class OddEvenMethod1 < public static void main (String args[])< Scanner scan=new Scanner(System.in); System.out.print("Enter the number for check odd or even: "); int num=scan.nextInt();//get input from user isEvenOrOdd(num); //call the method >static void isEvenOrOdd(int num)> 1) <<1) ==num) System.out.print(num+ " is a Even number"); else System.out.print(num+ " is a Odd number"); >>

When the above code is executed, it produces the following results

Enter the number for check odd or even: 501 501 is a Odd number
Enter the number for check odd or even: 200 200 is a Even number

Suggested for you

Источник

Java Program to Check Whether a Number is Even or Odd

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Check Whether a Number is Even or Odd

In this article we will see different ways to check whether a number is even or odd in Java. So, before going to the actual concept let’s first understand what is this even number and odd number then we will move to next part.

Even Number :

If a number is divisible by 2 and generates remainder of 0 then that number is called as even number.

14 is an even number because 14%2 returns remainder 0. 80 is an even number because 80%2 returns remainder 0.

Odd Number :

If a number is not divisible by 2 and does not generate remainder of 0 then that number is called as odd number.

33 is an odd number because 33%2 returns remainder 1. 17 is an odd number because 17%2 returns remainder 1.

Different ways to check whether a number is even or odd:

Method-1 : Using modulo(%) operator

By using the modulo operator also we can check a number is even or odd.

  • Divide the number by 2 modulo operator.
  • If the remainder is 0 then it is an even number and if the remainder is not equal to 0 then it is odd.

Let’s see the below program to understand how it actually works.

import java.util.*; class Check < public static void main(String args[]) < // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); //get the remainder using modulo operator int rem=num%2; // If the remainder is equals to 0 then it is even number // else it is odd number if(rem==0) < System.out.println("Even number"); >else < System.out.println("Odd number"); >> >
Output : Enter a number: 90 Even number //Another case Enter a number: 9 Odd number

Method-2 : Using division(/) operator

By using the division operator also we can check a number is even or odd.

  • Divide the number by division operator.
  • Then Again multiply 2 with quotient.
  • If quotient multiplied with 2 is equal to original number(dividend) then it is even.
  • If quotient multiplied with 2 is not equal to original number(dividend) then it is odd.
Example-1 Original number(dividend)=44 Then 44/2==22(quotient) Then 22*2=44(original number) So, it is even
Example-2 Original number(dividend)=33 Then 33/2==1(quotient) Then 1*2=2(Not original number) So, it is odd
import java.util.*; class Check < public static void main(String args[]) < // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); //using division operartor if((num/2) * 2==num) < System.out.println("Even number"); >else < System.out.println("Odd number"); >> >
Output: Enter a number: 44 Even number // Another case Enter a number: 33 Odd number

Method-3 : Using ternary operator

By using the ternary operator also we can check a number is even or odd.

Synatx: variable = Expression1 ? Expression2: Expression3

Let’s see the below program to understand how it actually works.

import java.util.*; class Check < public static void main(String args[]) < // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); // using ternary operator String res=(num%2==0) ? "Even number":"Odd number"; System.out.println(res); >>
Output: Enter a number: 44 Even number // Another case Enter a number: 33 Odd number

Method-4 : Using Bit wise AND(&) operator

By using the bit wise AND(&) operator also we can check a number is even or odd.

  • Perform the bit wise AND operation of the number with 1.
  • If the result is 1 then it is odd.
  • And if the result is 0 then it is even.

Let’s see the below program to understand how it actually works.

import java.util.*; class Check < public static void main(String args[]) < // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); // performing bit wise AND operation if ((num & 1) == 1) < System.out.println("Odd number"); >else < System.out.println("Even number"); >> >
Output: Enter a number: 44 Even number // Another case Enter a number: 33 Odd number

Method-5 : Using Bit wise OR(|) operator

By using the bit wise OR(&) operator also we can check a number is even or odd.

  • Perform the bit wise OR operation of the number with 1.
  • If the value of the number incremented by 1 then it is even.
  • And if the value remains unchanged then it is odd.

Let’s see the below program to understand how it actually works.

import java.util.*; class Check < public static void main(String args[]) < // Scanner class object created for input Scanner sc=new Scanner(System.in); System.out.print("Enter a number: "); //Take a number input from user int num=sc.nextInt(); // performing bit wise OR operation if ((num | 1) >num) < System.out.println("Even number"); >else < System.out.println("Odd number"); >> >
Output: Enter a number: 44 Even number // Another case Enter a number: 33 Odd number

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Basic Programs:

  • Java Program to Print an Integer (Entered by the User)
  • Java Program to Add Two Integers
  • Java Program to Multiply two Floating Point Numbers
  • Java Program to Find ASCII Value of a character
  • Java Program to Compute Quotient and Remainder
  • Java Program to Swap Two Numbers
  • Java Program to Find the Largest Among Three Numbers
  • Java Program to Find the Frequency of Character in a String
  • Java Program to Remove All Whitespaces from a String
  • Java Program to Round a Number to n Decimal Places
  • Java Program to Check if a String is Empty or Null

Источник

Оцените статью