Java Even Number Odd Number Example Program ( User Input )

preview_player
Показать описание
In this video, we will learn how to write a Java program to determine whether a number is even or odd. We will use the modulus operator (%) to check for the remainder when dividing the number by 2. If the remainder is 0, the number is even; otherwise, it is odd.

To start, we create a package named "example program" and import the Scanner class. We define a class called "even-odd" with the main function as the entry point. We declare two variables: "number" to store user input and "remainder" to store the calculated remainder.

The program prompts the user to enter a number, reads the input using Scanner, and calculates the remainder by dividing the number by 2. We then use an if-else statement to determine if the number is even or odd based on the remainder value.

Alternatively, we can simplify the code by using the ternary operator in a single line to check for even or odd numbers and display the result.

To run the program, enter an integer number, and it will output whether the number is even or odd. You can experiment with different numbers to verify the program's accuracy.

If you found this video helpful, please like and subscribe for more programming tutorials. Feel free to leave any comments or questions below. Thank you for watching!

our Social Media Pages

Our Website

#JavaProgramming #ExampleProgram #EvenOddNumber
Рекомендации по теме
Комментарии
Автор

I am an idiot. I literally listed all numbers 1-100 in switch case statement to produce a result that tells of it is an even or odd number. Luckily, the instructor acknowledged it. I didn't think of this method.

suyahatesntr
Автор

the only person that actually explains what he's doing😭😭 love you bro 🫶

hiraeth-gk
Автор

Thank you so much sir.... your explanation is clear

rajeshwaria
Автор

U just gained a new follower...great work keep it up

bruceintrepid
Автор

Thank you so much sir, I helps me a lot 😊

vinayaka
Автор

Sir your video is vary help fulll for me so thanks you so much sir you are omaging

blsbapun
Автор

Thank you so much sir! can you also do arrays on this sir?

hiddled
Автор

you left the { sign after the if bracket yeah hope am not the only one who saw that that...correct me if am wrong but its an error

xsxudrr
Автор

how can you do this but using a boolean

bosar
Автор

Is there a rattlesnake outside your window?

jkovert
Автор

import java.util.Scanner;
class EvenorOdd {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter your Number : ");
int userInput = scan.nextInt();
if (userInput % 2 == 0 ){
System.out.println("YOur Number is even");
}
else {
System.out.println("Your Number is odd ");
}
}
}

shehan