KEMBAR78
Simple Five Example of Netbean Java If Else Progra.. | PDF | Software Development | Computer Engineering
0% found this document useful (0 votes)
4 views3 pages

Simple Five Example of Netbean Java If Else Progra..

The document provides five simple Java if-else program examples that can be used in a NetBeans project. Each example illustrates a different application of conditional statements, including checking if a number is positive, determining voting eligibility, checking for even or odd numbers, comparing two numbers, and calculating a student's grade. The code snippets are ready for direct use in Java classes.

Uploaded by

Tesu Rathia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Simple Five Example of Netbean Java If Else Progra..

The document provides five simple Java if-else program examples that can be used in a NetBeans project. Each example illustrates a different application of conditional statements, including checking if a number is positive, determining voting eligibility, checking for even or odd numbers, comparing two numbers, and calculating a student's grade. The code snippets are ready for direct use in Java classes.

Uploaded by

Tesu Rathia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java if-else Programs in NetBeans

Here are five simple examples of Java if-else programs, each demonstrating a different
application of the conditional statement. You can copy and paste this code directly into a Java
class in a NetBeans project.

1. Check if a Number is Positive, Negative, or Zero


This program takes an integer input from the user and uses if-else-if to determine if it's positive,
negative, or zero.
import java.util.Scanner;​

public class NumberChecker {​
public static void main(String[] args) {​
Scanner scanner = new Scanner(System.in);​
System.out.print("Enter a number: ");​
int number = scanner.nextInt();​

if (number > 0) {​
System.out.println("The number is positive.");​
} else if (number < 0) {​
System.out.println("The number is negative.");​
} else {​
System.out.println("The number is zero.");​
}​
}​
}​

2. Determine if a Person is Eligible to Vote


This example checks a person's age to see if they are eligible to vote (assuming the voting age
is 18).
import java.util.Scanner;​

public class VotingEligibility {​
public static void main(String[] args) {​
Scanner scanner = new Scanner(System.in);​
System.out.print("Enter your age: ");​
int age = scanner.nextInt();​

if (age >= 18) {​
System.out.println("You are eligible to vote.");​
} else {​
System.out.println("You are not old enough to vote.");​
}​
}​
}​

3. Check for Even or Odd Number


This program uses the modulus operator (%) to check if a number is divisible by 2.
import java.util.Scanner;​

public class EvenOddChecker {​
public static void main(String[] args) {​
Scanner scanner = new Scanner(System.in);​
System.out.print("Enter an integer: ");​
int number = scanner.nextInt();​

if (number % 2 == 0) {​
System.out.println("The number is even.");​
} else {​
System.out.println("The number is odd.");​
}​
}​
}​

4. Compare Two Numbers


This simple program compares two numbers to see which is greater or if they are equal.
import java.util.Scanner;​

public class NumberComparison {​
public static void main(String[] args) {​
Scanner scanner = new Scanner(System.in);​
System.out.print("Enter the first number: ");​
int num1 = scanner.nextInt();​
System.out.print("Enter the second number: ");​
int num2 = scanner.nextInt();​

if (num1 > num2) {​
System.out.println(num1 + " is greater than " + num2);​
} else if (num2 > num1) {​
System.out.println(num2 + " is greater than " + num1);​
} else {​
System.out.println("The two numbers are equal.");​
}​
}​
}​
5. Simple Grade Calculator
This program calculates a student's grade based on a given score using multiple if-else-if
conditions.
import java.util.Scanner;​

public class GradeCalculator {​
public static void main(String[] args) {​
Scanner scanner = new Scanner(System.in);​
System.out.print("Enter the student's score: ");​
int score = scanner.nextInt();​

char grade;​
if (score >= 90) {​
grade = 'A';​
} else if (score >= 80) {​
grade = 'B';​
} else if (score >= 70) {​
grade = 'C';​
} else if (score >= 60) {​
grade = 'D';​
} else {​
grade = 'F';​
}​

System.out.println("The student's grade is: " + grade);​
}​
}​

You might also like