KEMBAR78
Java Program If Else Example | PDF
0% found this document useful (0 votes)
6 views2 pages

Java Program If Else Example

The document contains five Java programs that demonstrate basic programming concepts. These include checking if a number is even or odd, determining if a number is positive, negative, or zero, checking voting eligibility based on age, verifying if a year is a leap year, and finding the largest of three numbers. Each program includes a main method and conditional statements to perform the respective checks.

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)
6 views2 pages

Java Program If Else Example

The document contains five Java programs that demonstrate basic programming concepts. These include checking if a number is even or odd, determining if a number is positive, negative, or zero, checking voting eligibility based on age, verifying if a year is a leap year, and finding the largest of three numbers. Each program includes a main method and conditional statements to perform the respective checks.

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/ 2

1.

​ Even or Odd Number Checker


public class EvenOddChecker {
public static void main(String[] args) {
int number = 10;
if (number % 2 == 0) {
System.out.println(number + " is even");
} else {
System.out.println(number + " is odd");
}
}
}

2.​Positive, Negative, or Zero


public class NumberSignChecker {
public static void main(String[] args) {
int number = 0;
if (number > 0) {
System.out.println("Number is positive.");
} else if (number < 0) {
System.out.println("Number is negative.");
} else {
System.out.println("Number is zero.");
}
}
}

3.​Age Eligibility for Voting


public class VotingEligibility {
public static void main(String[] args) {
int age = 17;
if (age >= 18) {
System.out.println("Eligible to vote.");
} else {
System.out.println("Not eligible to vote.");
}
}
}

4.​ Leap Year Checker


public class LeapYearChecker {
public static void main(String[] args) {
int year = 2000;
boolean isLeapYear = false;
if (year % 4 == 0) {
isLeapYear = true;
if (year % 100 == 0 && year % 400 != 0) {
isLeapYear = false;
}
}
if (isLeapYear) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
}
}

5.​ Java Program: Find Largest of Three Numbers


public class LargestNumber {
public static void main(String[] args) {
int num1 = 10, num2 = 20, num3 = 7;

if (num1 >= num2 && num1 >= num3) {


System.out.println(num1 + " is the largest number.");
} else if (num2 >= num1 && num2 >= num3) {
System.out.println(num2 + " is the largest number.");
} else {
System.out.println(num3 + " is the largest number.");
}
}
}

You might also like