//// 1) if Statement Programs
//Program 1: Check if a number is positive
public class PositiveCheck {
public static void main(String[] args) {
int num = 5;
if (num > 0) {
System.out.println(num + " is positive.");
}
}
}
//Program 2: Check if a person is eligible to vote (age >= 18)
public class VotingEligibility {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
}
}
//Program 3: Check if a year is a leap year
public class LeapYearCheck {
public static void main(String[] args) {
int year = 2020;
if (year % 4 == 0) {
System.out.println(year + " is a leap year.");
}
}
}
//Program 4: Check if a character is uppercase
public class UpperCaseCheck {
public static void main(String[] args) {
char ch = 'A';
if (ch >= 'A' && ch <= 'Z') {
System.out.println(ch + " is an uppercase letter.");
}
}
}
//Program 5: Check if a number is a multiple of 10
public class MultipleOfTen {
public static void main(String[] args) {
int num = 30;
if (num % 10 == 0) {
System.out.println(num + " is a multiple of 10.");
}
}
}
//// 2) if..else Statement Programs
//Program 1: Check if a number is even or odd
public class EvenOddCheck {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}
//Program 2: Check if a person is a teenager (age between 13 and 19)
public class TeenagerCheck {
public static void main(String[] args) {
int age = 16;
if (age >= 13 && age <= 19) {
System.out.println("You are a teenager.");
} else {
System.out.println("You are not a teenager.");
}
}
}
//Program 3: Compare two numbers and print the larger one
public class LargerNumber {
public static void main(String[] args) {
int a = 5, b = 10;
if (a > b) {
System.out.println(a + " is larger.");
} else {
System.out.println(b + " is larger.");
}
}
}
//Program 4: Check if a number is positive, negative, or zero
public class NumberCheck {
public static void main(String[] args) {
int num = -3;
if (num > 0) {
System.out.println(num + " is positive.");
} else if (num < 0) {
System.out.println(num + " is negative.");
} else {
System.out.println(num + " is zero.");
}
}
}
//Program 5: Check if a person is eligible for a senior citizen discount (age >= 60)
public class SeniorDiscount {
public static void main(String[] args) {
int age = 65;
if (age >= 60) {
System.out.println("You are eligible for a senior citizen discount.");
} else {
System.out.println("You are not eligible for a senior citizen discount.");
}
}
}
//// 3) Nested if Statement Programs
//Program 1: Check if a number is positive and even
public class PositiveEvenCheck {
public static void main(String[] args) {
int num = 8;
if (num > 0) {
if (num % 2 == 0) {
System.out.println(num + " is positive and even.");
}
}
}
}
//Program 2: Check if a character is an uppercase vowel
public class UppercaseVowelCheck {
public static void main(String[] args) {
char ch = 'A';
if (ch >= 'A' && ch <= 'Z') {
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
System.out.println(ch + " is an uppercase vowel.");
}
}
}
}
//Program 3: Find the largest of three numbers
public class LargestOfThree {
public static void main(String[] args) {
int a = 5, b = 8, c = 3;
if (a > b && a > c) {
System.out.println(a + " is the largest.");
} else if (b > c) {
System.out.println(b + " is the largest.");
} else {
System.out.println(c + " is the largest.");
}
}
}
//Program 4: Check if a number is a multiple of both 5 and 10
public class MultipleOfFiveAndTen {
public static void main(String[] args) {
int num = 50;
if (num % 5 == 0) {
if (num % 10 == 0) {
System.out.println(num + " is a multiple of both 5 and 10.");
}
}
}
}
//Program 5: Determine the grade of a student based on their score
public class StudentGrade {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Grade A");
} else {
if (score >= 80) {
System.out.println("Grade B");
} else {
if (score >= 70) {
System.out.println("Grade C");
} else {
if (score >= 60) {
System.out.println("Grade D");
} else {
System.out.println("Grade F");
}
}
}
}
}
}
//Program 6 Check if a character is a vowel or consonant
public class VowelConsonantCheck {
public static void main(String[] args) {
char ch = 'E';
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
System.out.println(ch + " is a vowel.");
} else {
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is a consonant.");
}
}
}
}
//Program 7: Check if a number is divisible by both 2 and 3
public class DivisibilityCheck {
public static void main(String[] args) {
int num = 12;
if (num % 2 == 0) {
if (num % 3 == 0) {
System.out.println(num + " is divisible by both 2 and 3.");
} else {
System.out.println(num + " is divisible by 2 but not by 3.");
}
} else {
if (num % 3 == 0) {
System.out.println(num + " is divisible by 3 but not by 2.");
} else {
System.out.println(num + " is not divisible by either 2 or 3.");
}
}
}
}
//// 4) switch Statement Programs
//Program 1: Print the day of the week based on an integer input
public class DayOfWeek {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
//Program 2: Print the corresponding month name for a given month number
public class MonthName {
public static void main(String[] args) {
int month = 5;
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month");
}
}
}
// Program 3: Display a grade based on a given score
public class GradeCalculator {
public static void main(String[] args) {
int score = 85;
switch (score / 10) {
case 10:
case 9:
System.out.println("Grade A");
break;
case 8:
System.out.println("Grade B");
break;
case 7:
System.out.println("Grade C");
break;
case 6:
System.out.println("Grade D");
break;
default:
System.out.println("Grade F");
}
}
}
//Program 4: Perform basic arithmetic operations based on user input
public class ArithmeticOperations {
public static void main(String[] args) {
char operator = '+';
int a = 10, b = 5;
switch (operator) {
case '+':
System.out.println("Result: " + (a + b));
break;
case '-':
System.out.println("Result: " + (a - b));
break;
case '*':
System.out.println("Result: " + (a * b));
break;
case '/':
System.out.println("Result: " + (a / b));
break;
default:
System.out.println("Invalid operator");
}
}
}
//Program 5: Print the season based on the month number
public class SeasonCheck {
public static void main(String[] args) {
int month = 4;
switch (month) {
case 12:
case 1:
case 2:
System.out.println("Winter");
break;
case 3:
case 4:
case 5:
System.out.println("Spring");
break;
case 6:
case 7:
case 8:
System.out.println("Summer");
break;
case 9:
case 10:
case 11:
System.out.println("Autumn");
break;
default:
System.out.println("Invalid month");
}
}
}