Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Java Code Example for Even Number

The document provides a Java code example for checking if a number is even or odd using the Scanner class for user input. It explains key components of the code, including the main method's structure, access modifiers, and the use of the static keyword. Additionally, it highlights the importance of closing the Scanner to prevent resource leaks.

Uploaded by

kvkumaravel28
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Java Code Example for Even Number

The document provides a Java code example for checking if a number is even or odd using the Scanner class for user input. It explains key components of the code, including the main method's structure, access modifiers, and the use of the static keyword. Additionally, it highlights the importance of closing the Scanner to prevent resource leaks.

Uploaded by

kvkumaravel28
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Java Code Example for

even number
import java.util.Scanner; // Import Scanner for user input

public class EvenNumberCheck {


public static void main(String[] args) {
// Create Scanner object to get input from user
Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");


int number = scanner.nextInt(); // Read user input

// Check if the number is even or odd


if (number % 2 == 0) {
System.out.println(number + " is an even number.");
} else {
System.out.println(number + " is an odd number.");
}
scanner.close(); // Close the scanner
}
Explanation of the Code

o Scanner: The Scanner class is used to take user input from the console.

o if Statement: The program checks if the number is divisible by 2:


 number % 2 == 0 checks if the remainder is 0 (even).

 If true, the program prints that the number is even.

 If false, it prints that the number is odd.

o Close Scanner: After using the scanner, it’s good practice to close it to avoid resource leaks.
• The statement public static void main(String[] args) is the entry point of any Java application. Let's break down what each part
means:

• public

 Visibility Modifier: public is an access modifier. It means that this method can be accessed from anywhere, meaning there are
no restrictions on who can call this method.

 In simple terms, public ensures that the main method can be accessed by the Java runtime to start your program.

• static

 Static Keyword: static means that this method belongs to the class, not to any specific instance of the class.

 The Java runtime can call this method without creating an instance (object) of the class. It makes it accessible directly through
the class.

 Since main is static, it can be invoked without creating an object of the class.

• 3. void

 Return Type: void means that this method does not return any value. The main method is simply used to start the program,
• main

 Method Name: main is the name of the method. This is a special method name in Java that the Java Virtual
Machine (JVM) looks for when you run your program.

 The JVM starts execution from the main method in your program.

• 5. String[] args

 Parameter: String[] args is a parameter of type array of Strings.


o String[] means it is an array of Strings. The square brackets [] indicate that it is an array.

• args is a name commonly used to represent command-line arguments that you pass when running the
program. These are typically input values that the user provides when executing the program from the
command line or terminal

You might also like