Here are 10 different Java programs using different conditional statements. Each one uses a unique condition and demonstrates various if
, if-else
, else-if
, and nested condition logics:
1. Check if Number is Positive
int num = 5;
if (num > 0) {
System.out.println("Positive Number");
}
2. Check if Number is Even or Odd
int num = 7;
if (num % 2 == 0) {
System.out.println("Even Number");
} else {
System.out.println("Odd Number");
}
3. Check if Age is Eligible to Vote
int age = 18;
if (age >= 18) {
System.out.println("Eligible to Vote");
} else {
System.out.println("Not Eligible");
}
4. Check if Number is Positive, Negative or Zero
int num = 0;
if (num > 0) {
System.out.println("Positive");
} else if (num < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
5. Find Largest of Two Numbers
int a = 15, b = 20;
if (a > b) {
System.out.println("a is larger");
} else {
System.out.println("b is larger");
}
6. Check if Year is Leap Year
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
System.out.println("Leap Year");
} else {
System.out.println("Not a Leap Year");
}
7. Check if Character is Vowel or Consonant
char ch = 'e';
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
System.out.println("Vowel");
} else {
System.out.println("Consonant");
}
8. Check if Student Passes (Nested If)
int marks = 75;
if (marks >= 50) {
if (marks >= 90) {
System.out.println("Excellent!");
} else {
System.out.println("Passed");
}
} else {
System.out.println("Failed");
}
9. Grade System Using else-if Ladder
int marks = 82;
if (marks >= 90) {
System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 60) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
10. Check if Number is Divisible by 3 and 5
int num = 15;
if (num % 3 == 0 && num % 5 == 0) {
System.out.println("Divisible by 3 and 5");
} else {
System.out.println("Not Divisible by both");
}