JAVA PROGRAMMING
Q1. WAP to add three numbers.
public class AddThreeNumbers {
public static void main(String[] args) {
// Declare three variables to hold the numbers
int num1 = 10;
int num2 = 20;
int num3 = 30;
// Calculate the sum of the three numbers
int sum = num1 + num2 + num3;
// Print the result
System.out.println("The sum of the three numbers is: " + sum);
}
Q2. WAP to convert Celsius to Fahrenheit.
import java.util.Scanner;
public class FahrenheitToCelsius {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Ask the user to enter a temperature in Fahrenheit
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = scanner.nextDouble();
// Convert Fahrenheit to Celsius using the formula
double celsius = (fahrenheit - 32) * 5 / 9;
// Print the result
System.out.println("Temperature in Celsius: " + celsius);
}
Q3. WAP to find the area and perimeter of a rectangle.
import java.util.Scanner;
public class Rectangle {
public static void main(String[] args) {
// Create a Scanner object to read input from the user
Scanner scanner = new Scanner(System.in);
// Ask the user to enter the length and width of the rectangle
System.out.print("Enter the length of the rectangle: ");
double length = scanner.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = scanner.nextDouble();
// Calculate the area of the rectangle
double area = length * width;
// Calculate the perimeter of the rectangle
double perimeter = 2 * (length + width);
// Print the results
System.out.println("Area of the rectangle: " + area);
System.out.println("Perimeter of the rectangle: " + perimeter);
}
Q4.WAP to find the number is odd or even.
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.");
}
Q5. WAP to find the largest number.
import java.util.Scanner;
public class LargestNumber {
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 larger.");
} else if (num2 > num1) {
System.out.println(num2 + " is larger.");
} else {
System.out.println("Both numbers are equal.");
}
Q6. Check if a person is eligible to vote
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 eligible to vote.");
}
Q7. Check if a character is a vowel or consonant
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 ch = scanner.next().charAt(0);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
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.");
}
Q8. Check if a number is positive, negative, or zero
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(number + " is zero.");
}
Q9. Grade Classification Based on Marks
import java.util.Scanner;
public class GradeClassification {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your marks: ");
int marks = scanner.nextInt();
if (marks >= 90) {
System.out.println("Grade: A+");
} else if (marks >= 80) {
System.out.println("Grade: A");
} else if (marks >= 70) {
System.out.println("Grade: B+");
} else if (marks >= 60) {
System.out.println("Grade: B");
} else if (marks >= 50) {
System.out.println("Grade: C");
} else if (marks >= 40) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}
Q10. Determine the Type of Triangle
import java.util.Scanner;
public class TriangleType {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the length of the first side: ");
int side1 = scanner.nextInt();
System.out.print("Enter the length of the second side: ");
int side2 = scanner.nextInt();
System.out.print("Enter the length of the third side: ");
int side3 = scanner.nextInt();
if (side1 == side2 && side2 == side3) {
System.out.println("The triangle is Equilateral.");
} else if (side1 == side2 || side2 == side3 || side1 == side3) {
System.out.println("The triangle is Isosceles.");
} else {
System.out.println("The triangle is Scalene.");
}
Q11. Determine the Day of the Week
import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number (1-7) to represent a day of the week: ");
int day = scanner.nextInt();
if (day == 1) {
System.out.println("It's Monday.");
} else if (day == 2) {
System.out.println("It's Tuesday.");
} else if (day == 3) {
System.out.println("It's Wednesday.");
} else if (day == 4) {
System.out.println("It's Thursday.");
} else if (day == 5) {
System.out.println("It's Friday.");
} else if (day == 6) {
System.out.println("It's Saturday.");
} else if (day == 7) {
System.out.println("It's Sunday.");
} else {
System.out.println("Invalid input! Please enter a number between 1 and 7.");
}
Q12. Simple Calculator Using Switch Case
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number:");
double num1 = scanner.nextDouble();
System.out.println("Enter second number:");
double num2 = scanner.nextDouble();
System.out.println("Choose operation: +, -, *, /");
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 is not allowed.");
}
break;
default:
System.out.println("Invalid operator. Please choose +, -, *, or /.");
}
scanner.close();
}
}
Q13. Day of the Week Converter Using Switch Case
import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a number (1-7):");
int dayNumber = scanner.nextInt();
String dayName;
switch (dayNumber) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day number. Please enter a number between 1 and 7.";
System.out.println("Day: " + dayName);
scanner.close();