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