1. Create a program to determine if a number is even or odd?
Algorithm :
- Start the Program.
- Create Scanner Object:
- Used to take input from the user.
- Prompt User for Input:
- System.out.print(“Enter a number: “);
- Read Integer Input:
- int number = scanner.nextInt();
- Check if Number is Even or Odd:
- Use the condition number % 2 == 0:
- If true, print that the number is even.
- If false, print that the number is odd.
- Close Scanner:
- scanner.close();
- int number = scanner.nextInt();
- End Program.
Program :
import java.util.Scanner; public class EvenOdd { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a number: “); int number = scanner.nextInt(); if (number % 2 == 0) { System.out.println(number + ” is even.”); } else { System.out.println(number + ” is odd.”); } scanner.close(); } } |
Output :
Enter a number: 4 4 is even. Enter a number: 7 7 is odd. |
Explanation :
This Java program takes an integer input from the user and checks whether it is even or odd. It uses the modulus operator % to determine if the number is divisible by 2. If the remainder is zero, the number is even; otherwise, it is odd. The program uses the Scanner class to read user input and then outputs the result accordingly, demonstrating basic input handling and conditional logic. |
2. Write a program that prints whether a number is positive, negative, or zero using if-else statements?
Algorithm :
- Start the Program.
- Create Scanner Object:
- Used to read input from the user.
- Prompt User for Input:
- Display message: “Enter a number: “
- Read Integer Input:
- int number = scanner.nextInt();
- Check the Value of the Number:
- If number > 0:
- → Print: “number is positive.”Else if number < 0:
- → Print: “number is negative.”
- Else:
→ Print: “The number is zero.”
- If number > 0:
- Close the Scanner:
- scanner.close();
- End the Program.
Program :
import java.util.Scanner; public class PositiveNegativeZero { 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(number + ” is positive.”); } else if (number < 0) { System.out.println(number + ” is negative.”); } else { System.out.println(“The number is zero.”); } scanner.close(); } } |
Output :
Enter a number: 10 10 is positive. Enter a number: -5 -5 is negative. Enter a number: 0 The number is zero. |
Explanation :
This Java program reads an integer input from the user and determines whether it is positive, negative, or zero. It uses conditional if-else if-else statements to evaluate the number’s value. Based on the result, it prints the appropriate message. The program uses the Scanner class for input, showcasing basic control flow and decision-making in Java. |
3. Write a program to find the largest of three numbers using nested if statements?
Algorithm :
- Start the Program.
- Create Scanner Object:
- Used to take input from the user.
- Prompt and Read Three Numbers:
- Ask user to enter first number → store in num1.
- Ask user to enter second number → store in num2.
- Ask user to enter third number → store in num3.
- Compare Numbers Using Nested if Statements:
- If num1 >= num2 :
- Then check if num1 >= num3:
- If true → num1 is largest.
- Else → num3 is largest.
- Then check if num1 >= num3:
- Else:
- Check if num2 >= num3:
- If true → num2 is largest.
- Else → num3 is largest.
- Check if num2 >= num3:
- If num1 >= num2 :
- Print the Largest Number.
- Close Scanner.
- End Program.
Program :
import java.util.Scanner; public class LargestOfThree { 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(); System.out.print(“Enter the third number: “); int num3 = scanner.nextInt(); if (num1 >= num2) { if (num1 >= num3) { System.out.println(num1 + ” is the largest.”); } else { System.out.println(num3 + ” is the largest.”); } } else { if (num2 >= num3) { System.out.println(num2 + ” is the largest.”); } else { System.out.println(num3 + ” is the largest.”); } } scanner.close(); } } |
Output :
User inputs: Enter the first number: 15 Enter the second number: 2 Enter the third number: 10 22 is the largest. |
Explanation :
This Java program reads three integers from the user and determines the largest among them using nested if conditions. It compares the first number with the second and then the greater of those with the third to find the overall maximum. The result is printed to the console. The program demonstrates user input handling and decision-making using nested if statements in Java. |
4. Write a program to determine whether a given year is a leap year using conditional statements?
Algorithm :
- Start the Program.
- Create a Scanner Object:
- Used to take input from the user.
- Prompt the User for Input:
- Display message: “Enter a year: “
- Read the Input Year:
- Store the user input in the variable year.
- Check Leap Year Condition Using Logic:
- If (year is divisible by 4 AND not divisible by 100)
- OR
- (year is divisible by 400):
- Then it’s a leap year → Print: “year is a leap year.”
- Else:
- Not a leap year → Print: “year is not a leap year.”
- Close the Scanner.
- End the Program.
Program :
import java.util.Scanner; public class LeapYear { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a year: “); int year = scanner.nextInt(); if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { System.out.println(year + ” is a leap year.”); } else { System.out.println(year + ” is not a leap year.”); } scanner.close(); } } |
Output :
Enter a year: 2024 2024 is a leap year. |
Explanation :
This Java program checks if a given year is a leap year. It uses a standard condition: a year is a leap year if it’s divisible by 4 but not by 100, unless it’s also divisible by 400. The user enters a year, and based on this condition, the program prints whether it is a leap year or not. It demonstrates logical operators and condition checking using real-world rules. |
5. Write a program to check if a number is a perfect number using if statements?
Algorithm :
- Start the Program.
- Initialize a Number:
- Assign a number (e.g., number = 28).
- Initialize a Sum Variable:
- sum = 0 (to hold the sum of proper divisors).
- Loop Through All Numbers from 1 to (number – 1):
- Use a for loop: for (int i = 1; i < number; i++)
- Inside the loop:
- If number % i == 0 (i is a divisor), add i to sum.
- Check If the Sum of Divisors Equals the Original Number:
- If sum == number, print that it is a perfect number.
- Else, print that it is not a perfect number.
- End the Program.
Program :
public class PerfectNumber { public static void main(String[] args) { int number = 28; int sum = 0; for (int i = 1; i < number; i++) { if (number % i == 0) { sum += i; } } if (sum == number) { System.out.println(number + ” is a perfect number.”); } else { System.out.println(number + ” is not a perfect number.”); } } } |
Output :
28 is a perfect number. |
Explanation :
This Java program checks if a given number is a perfect number, meaning the sum of its proper divisors (excluding itself) equals the number. For example, 28 has divisors 1, 2, 4, 7, and 14, which sum to 28. The program uses a loop to find all such divisors and adds them. If the total matches the original number, it prints that the number is perfect. This demonstrates the use of loops, conditionals, and modulus for divisor checks. |
6. Write a program that accepts an integer and prints whether it is divisible by 2, 3, or 5 using multiple if conditions?
Algorithm :
- Start the Program.
- Initialize a Number:
- Assign a value to number (e.g., number = 30).
- Check Divisibility:
- If number % 2 == 0, print it is divisible by 2.
- If number % 3 == 0, print it is divisible by 3.
- If number % 5 == 0, print it is divisible by 5.
- Check If Not Divisible by 2, 3, or 5:
- If none of the above conditions are true, print that it is not divisible by 2, 3, or 5.
- End the Program.
Program :
public class DivisibilityCheck { public static void main(String[] args) { int number = 30; // You can change this number to test other cases if (number % 2 == 0) { System.out.println(number + ” is divisible by 2.”); } if (number % 3 == 0) { System.out.println(number + ” is divisible by 3.”); } if (number % 5 == 0) { System.out.println(number + ” is divisible by 5.”); } if (number % 2 != 0 && number % 3 != 0 && number % 5 != 0) { System.out.println(number + ” is not divisible by 2, 3, or 5.”); } } } |
Output :
30 is divisible by 2. 30 is divisible by 3. 30 is divisible by 5. |
Explanation :
This Java program checks whether a given number is divisible by 2, 3, or 5 using modulus operations. It prints messages for each condition that is true. If the number is not divisible by any of these, it outputs a separate message. For example, with number = 30, the program confirms it’s divisible by all three. This program demonstrates simple decision-making and the use of logical and arithmetic operators. |
7. Write a program that displays the day of the week for a given number (1 for Sunday, 2 for Monday, etc.) using a switch statement?
Algorithm :
- Start the Program.
- Initialize dayNumber:
- Assign a value to dayNumber (e.g., 4).
- Use switch Statement to Match the Day:
- Check the value of dayNumber:
- If 1, print “Sunday”
- If 2, print “Monday”
- If 3, print “Tuesday”
- If 4, print “Wednesday”
- If 5, print “Thursday”
- If 6, print “Friday”
- If 7, print “Saturday”
- Check the value of dayNumber:
- If the value doesn’t match 1–7, go to default case and print “Invalid day number.”
- End the Program.
Program :
public class DayOfWeek { public static void main(String[] args) { int dayNumber = 4; switch (dayNumber) { case 1: System.out.println(“Sunday”); break; case 2: System.out.println(“Monday”); break; case 3: System.out.println(“Tuesday”); break; case 4: System.out.println(“Wednesday”); break; case 5: System.out.println(“Thursday”); break; case 6: System.out.println(“Friday”); break; case 7: System.out.println(“Saturday”); break; default: System.out.println(“Invalid day number.”); } } } |
Output :
Wednesday |
Explanation :
This Java program maps a numeric value (1–7) to a day of the week using a switch statement. The variable dayNumber is checked against each case, and the corresponding day name is printed. If the number is outside the valid range (1–7), it prints “Invalid day number.” It efficiently demonstrates the use of switch-case for decision-making based on discrete values. |
8. Write a program to check whether a given character is a digit or not?
Algorithm :
- Start the Program.
- Initialize a Character:
- Assign a character (e.g., ‘5’) to the variable character.
- Check if the Character is a Digit:
- Use Character.isDigit(character) to verify if the character is a digit (0–9).
- Display the Result:
- If the check is true, print that the character is a digit.
- Otherwise, print that it is not a digit.
- End the Program.
Program :
public class DigitCheck { public static void main(String[] args) { char character = ‘5’; if (Character.isDigit(character)) { System.out.println(character + ” is a digit.”); } else { System.out.println(character + ” is not a digit.”); } } } |
Output :
5 is a digit. |
Explanation :
This Java program checks whether a given character is a numeric digit using the built-in method Character.isDigit(). If the character is between ‘0’ and ‘9’, it prints that it’s a digit; otherwise, it indicates it is not. This program shows a simple way to classify characters using Java’s standard library functions. |
9. Write a program to check if a character is a vowel or consonant using an `if-else` statement?
Algorithm :
- Start the Program.
- Create a Scanner Object:
- To accept user input from the keyboard.
- Prompt User for a Character:
- Display “Enter a character:”.
- Read the Character Input:
- Use scanner.next().charAt(0) to get the first character from user input.
- Check if the Character is a Vowel:
- Compare the character with both lowercase and uppercase vowels (a, e, i, o, u, A, E, I, O, U).
- Display Result:
- If it matches any vowel, print it is a vowel.
- Otherwise, print it is a consonant.
- Close the Scanner.
- End the Program.
Program :
import java.util.Scanner; public class VowelConsonant { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a character: “); char character = scanner.next().charAt(0); if (character == ‘a’ || character == ‘e’ || character == ‘i’ || character == ‘o’ || character == ‘u’ || character == ‘A’ || character == ‘E’ || character == ‘I’ || character == ‘O’ || character == ‘U’) { System.out.println(character + ” is a vowel.”); } else { System.out.println(character + ” is a consonant.”); } scanner.close(); } } |
Output :
Enter a character: a a is a vowel. |
Explanation :
This Java program takes a character as input and checks whether it is a vowel or consonant. It uses a series of if conditions to compare the character against both lowercase and uppercase vowels. If the character matches any of them, it prints that it’s a vowel; otherwise, it prints that it’s a consonant. It demonstrates basic input handling and conditional logic in Java. |
10. Write a program to check whether a given character contains upper case alphabet or not?
Algorithm :
- Start the Program.
- Create a Scanner Object
- To read input from the user.
- Prompt the User
- Display the message: “Enter a character:”.
- Read the Character Input
- Use scanner.next().charAt(0) to read the first character entered by the user.
- Check if the Character is Uppercase
- Use Character.isUpperCase(character) to check if the character is an uppercase letter (A–Z).
- Display the Result
- If it is uppercase, print “character is an uppercase letter.”
- Otherwise, print “character is not an uppercase letter.”
- Close the Scanner.
- End the Program.
Program :
import java.util.Scanner; public class UpperCaseCheck { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter a character: “); char character = scanner.next().charAt(0); if (Character.isUpperCase(character)) { System.out.println(character + ” is an uppercase letter.”); } else { System.out.println(character + ” is not an uppercase letter.”); } scanner.close(); } } |
Output :
Enter a character: A A is an uppercase letter. Enter a character: b b is not an uppercase letter. |
Explanation :
This Java program reads a single character from the user and checks whether it is an uppercase letter using the built-in method Character.isUpperCase(). If the character is in the range A–Z, it confirms that it’s uppercase; otherwise, it reports that it is not. The program demonstrates how to handle character input and perform basic character classification. |
11. Write a program to determine if a number is a palindrome using conditional statements?
Algorithm :
- Start the Program.
- Initialize Variables:
- Set an integer number (e.g., 121).
- Store originalNumber = number.
- Set reversedNumber = 0 to build the reverse of the number.
- Reverse the Number (Using while loop):
- While number is not 0:
- Get the last digit using number % 10 and store it in remainder.
- Append the digit to reversedNumber by reversedNumber = reversedNumber * 10 + remainder.
- Remove the last digit by number = number / 10.
- While number is not 0:
- Compare Original and Reversed Numbers:
- If originalNumber == reversedNumber, print it’s a palindrome.
- Else, print it’s not a palindrome.
- End the Program.
Program :
public class PalindromeCheck { public static void main(String[] args) { int number = 121; int originalNumber = number; int reversedNumber = 0; int remainder; while (number != 0) { remainder = number % 10; reversedNumber = reversedNumber * 10 + remainder; number /= 10; } if (originalNumber == reversedNumber) { System.out.println(originalNumber + ” is a palindrome.”); } else { System.out.println(originalNumber + ” is not a palindrome.”); } } } |
Output :
121 is a palindrome. |
Explanation :
This Java program checks if a given integer is a palindrome, meaning it reads the same forwards and backwards. It reverses the original number using a loop and compares the reversed number with the original. If both are equal, it prints that the number is a palindrome; otherwise, it prints that it’s not. This demonstrates the use of loops, arithmetic operations, and condition checking. |
12. Write a program to a grade classification based on marks by using conditional statements ?
Algorithm :
- Start the program.
- Initialize marks variable with a predefined value (e.g., 85).
- Declare a String variable named grade.
- Check the value of marks using if-else conditions:
- If marks >= 90, set grade = “A”.
- Else if marks >= 80, set grade = “B”.
- Else if marks >= 70, set grade = “C”.
- Else if marks >= 60, set grade = “D”.
- Else, set grade = “Fail”.
- Print the grade using System.out.println.
- End the program.
Program :
public class GradeClassification { public static void main(String[] args) { // Predefined marks double marks = 85; String grade; if (marks >= 90) { grade = “A”; } else if (marks >= 80) { grade = “B”; } else if (marks >= 70) { grade = “C”; } else if (marks >= 60) { grade = “D”; } else { grade = “Fail”; } System.out.println(“The grade is: ” + grade); } } |
Output :
The grade is: B |
Explanation :
This program classifies a student’s grade based on predefined marks using a series of if-else conditions. It compares the marks against specific thresholds and assigns a corresponding grade from A to D, or “Fail” if below 60. For example, with 85 marks, the program prints grade B. This demonstrates basic conditional logic in Java. |
13. Write a program to check if number is in Range 10 to 50 or not?
Algorithm :
- Start the program.
- Initialize an integer variable number with a predefined value (e.g., 35).
- Check if the number is between 10 and 50 using the condition:
- if (number >= 10 && number <= 50)
- If true, print “Number is in range”.
- Otherwise, print “Number is out of range”.
- End the program
Program :
public class NumberRangeCheck { public static void main(String[] args) { int number = 35; if (number >= 10 && number <= 50) { System.out.println(“Number is in range”); } else { System.out.println(“Number is in out of range”); } } } |
Output :
Number is in range |
Explanation :
This program checks whether a predefined number lies within the range of 10 to 50 using a simple conditional check. If the number satisfies the range condition, it confirms it is in range; otherwise, it indicates it is out of range. In this case, since 35 is between 10 and 50, it prints “Number is in range.” |
14. Write a program to determine the type of vowel (uppercase or lowercase) using switch case?
Algorithm :
- Start the program.
- Declare a char variable vowel and assign it a value (‘E’).
- Use a switch statement to check the value of vowel:
- If it matches ‘a’, ‘e’, ‘i’, ‘o’, or ‘u’, print that it is a lowercase vowel.
- If it matches ‘A’, ‘E’, ‘I’, ‘O’, or ‘U’, print that it is an uppercase vowel.
- For any other character, print that it is not a vowel.
- End the program.
Program :
public class VowelTypeCheck { public static void main(String[] args) { char vowel = ‘E’; switch (vowel) { case ‘a’: case ‘e’: case ‘i’: case ‘o’: case ‘u’: System.out.println(vowel + ” is a lowercase vowel.”); break; case ‘A’: case ‘E’: case ‘I’: case ‘O’: case ‘U’: System.out.println(vowel + ” is an uppercase vowel.”); break; default: System.out.println(vowel + ” is not a vowel.”); } } } |
Output :
E is an uppercase vowel. |
Explanation :
This program uses a switch statement to determine if a given character is a vowel and whether it is uppercase or lowercase. It prints the appropriate message based on the case of the vowel. In this case, ‘E’ is an uppercase vowel, so it prints: “E is an uppercase vowel.” |
15. Write a program to implement a simple calculator using switch case that performs addition, subtraction, multiplication, and division based on user-defined operation (+, -, *, /)?
Algorithm :
- Start the program.
- Import the Scanner class to read user input.
- Create a Scanner object to read input from the user.
- Prompt the user to enter the first number, and store it in num1.
- Prompt the user to enter the second number, and store it in num2.
- Prompt the user to enter an operator (+, -, *, /) and store it in the variable operator.
- Use a switch statement to perform the operation based on the value of operator:
- Case ‘+’: Add num1 and num2, display the result.
- Case ‘-‘: Subtract num2 from num1, display the result.
- Case ‘*’: Multiply num1 and num2, display the result.
- Case ‘/’:
- If num2 != 0, divide num1 by num2 and display the result.
- Else, display an error message for division by zero.
- Default case: Display a message saying the operator is invalid.
- Close the scanner.
- End the program.
Program :
import java.util.Scanner; public class SimpleCalculator { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(“Enter the first number: “); double num1 = scanner.nextDouble(); System.out.print(“Enter the second number: “); double num2 = scanner.nextDouble(); System.out.print(“Enter an operator (+, -, *, /): “); char operator = scanner.next().charAt(0); double result; switch (operator) { case ‘+’: result = num1 + num2; System.out.println(“Result: ” + result); break; case ‘-‘: result = num1 – num2; System.out.println(“Result: ” + result); break; case ‘*’: result = num1 * num2; System.out.println(“Result: ” + result); break; case ‘/’: if (num2 != 0) { result = num1 / num2; System.out.println(“Result: ” + result); } else { System.out.println(“Error: Division by zero.”); } break; default: System.out.println(“Invalid operator.”); scanner.close(); } } |
Output :
Enter the first number: 10 Enter the second number: 5 Enter an operator (+, -, *, /): + Result: 15.0 |
Explanation :
This is a simple calculator program that performs basic arithmetic operations (+, -, *, /) based on user input. It handles invalid operators and prevents division by zero. For example, if you enter 10, 5, and *, it outputs Result: 50.0. |
16. Write a program to find the number of days in a month using switchcase?
Algorithm :
- Start the program.
- Declare an integer variable month and assign it a value (e.g., 2).
- Use a switch statement to check the value of month.
- Match the month value to its corresponding case:
- case 1 → Print “January has 31 days.”
- case 2 → Print “February has 28 days.”
- case 3 → Print “March has 31 days.”
- And so on, up to case 12 for December.
- If month doesn’t match any case (1–12):
- Execute the default case and print an error message.
- End the program.
Program :
public class DaysInMonth { public static void main(String[] args) { int month = 2; switch (month) { case 1: System.out.println(“January has 31 days.”); break; case 2: System.out.println(“February has 28 days.”); break; case 3: System.out.println(“March has 31 days.”); break; case 4: System.out.println(“April has 30 days.”); break; case 5: System.out.println(“May has 31 days.”); break; case 6: System.out.println(“June has 30 days.”); break; case 7: System.out.println(“July has 31 days.”); break; case 8: System.out.println(“August has 31 days.”); break; case 9: System.out.println(“September has 30 days.”); break; case 10: System.out.println(“October has 31 days.”); break; case 11: System.out.println(“November has 30 days.”); break; case 12: System.out.println(“December has 31 days.”); break; default: System.out.println(“Invalid month number. Please enter a number from 1 to 12.”); } } } |
Output :
February has 28 days. |
Explanation :
This program takes a predefined month number (1–12) and uses a switch statement to print the number of days in that month. For example, if month = 2, it outputs: “February has 28 days.” It also includes a default case to handle invalid inputs outside the 1–12 range. |