Java Lab Manual with Solution 1
Java Lab Manual with Solution 1
G MAHAVIDYALAYA
Java Technology
Assignments-1
Q1. Write a Java program to demonstrate automatic type conversion from int to float and double.
Output
Integer value: 10
Converted to float: 10.0
Converted to double: 10.0
Q2. Write a program to demonstrate explicit type casting from double to int and observe the data loss.
Q3. Write a program to accept two numbers from the user and perform all arithmetic operations (+, -, *, /, %).
import java.util.Scanner;
OUTPUT
Enter the first number: 10
Enter the second number: 2
Sum: 12.0
Difference: 8.0
Product: 20.0
Quotient: 5.0
Modulus: 0.0
Q4. Write a program that takes three numbers from the user and determines which one is the greatest using
relational and logical operators.
import java.util.Scanner;
Output
Enter the first number: 15
Enter the second number: 25
Enter the third number: 20
The greatest number is: 25
Q5. Write a Java program that demonstrates both pre-increment and post-increment behavior.
// Pre-increment
System.out.println("Initial value of a: " + a);
int preIncrement = ++a; // Increment a, then assign to preIncrement
System.out.println("After pre-increment, a: " + a);
System.out.println("Value of preIncrement: " + preIncrement);
// Post-increment
System.out.println("\nInitial value of b: " + b);
int postIncrement = b++; // Assign b to postIncrement, then increment b
System.out.println("After post-increment, b: " + b);
System.out.println("Value of postIncrement: " + postIncrement);
}
}
OUTPUT
Initial value of a: 5
After pre-increment, a: 6
Value of preIncrement: 6
Initial value of b: 5
After post-increment, b: 6
Value of postIncrement: 5
Q6. Write a Java program that checks if a number entered by the user is even or odd using the if-else
statement.
import java.util.Scanner;
OUTPUT
Enter a number: 7
7 is odd.
Q7. Write a Java program that accepts the age of a person and checks whether the person is eligible to vote
(age >= 18). Use nested if statements to check if the person is a minor or an adult.
import java.util.Scanner;
OUTPUT
Q8. Write a Java program that accepts a number between 1 and 7 and prints the corresponding day of the
week using the switch statement.
import java.util.Scanner;
OUTPUT
Q9. Write a Java program that accepts marks from the user and assigns a grade based on the following
criteria:
Marks >= 90: A
Marks >= 80 and < 90: B
Marks >= 70 and < 80: C
Marks >= 60 and < 70: D
Marks < 60: F
import java.util.Scanner;
OUTPUT
Q10. Write a program that accepts a year from the user and checks whether it is a leap year using the
conditional (?:) operator.
import java.util.Scanner;
OUTPUT
Enter a year: 2024
2024 is a leap year.
Q11. Write a Java program that accepts two numbers and an operator (+, -, *, /) from the user and performs
the corresponding operation using the switch statement.
import java.util.Scanner;
Q12. Write a Java program to find the sum of the first n natural numbers using a for loop.
import java.util.Scanner;
OUTPUT
Q13. Write a Java program to calculate the factorial of a number using a while loop.
import java.util.Scanner;
OUTPUT
Q14. Write a Java program that takes an integer input from the user and checks whether the number is prime
using a for loop.
import java.util.Scanner;
OUTPUT
Enter an integer: 7
7 is a prime number.
Q15. Write a Java program that prints the Fibonacci series up to n terms using a while loop.
import java.util.Scanner;
OUTPUT
Q16. Write a program to reverse the digits of a number using a while loop.
Q17. Write a Java program to print the multiplication table of a given number using a for loop.
Q18. Write a Java program using nested loops to print the following pattern:
1
12
123
1234
Q19. Write a Java program that takes an integer input and calculates the sum of its digits using a do-while
loop.
Q20. Write a Java program to check if a number is a palindrome or not using a while loop.
Q21. Write a Java program to count the number of vowels in a given string using a for loop.
Q22. Write a Java program that prints the numbers from 1 to 10. Use a break statement to stop the loop when
the number is equal to 7.
Q23. Write a Java program that prints numbers from 1 to 10, but uses the continue statement to skip printing
the number 4.
Q24. Write a Java program that uses a switch statement to display the name of a month based on its number
(1 for January, 2 for February, etc.). Use break to terminate each case.
Q25. Write a Java program that defines a method to find the maximum of two numbers using the return
Q26. Write a Java program that uses both break and continue in the same loop to print numbers from 1 to 10
but stops the loop if the number is 8 and skips the number 5.
Q27. Create a Java class Student with attributes name, rollNumber, and marks. Add methods to input and
display the student’s details. Create objects of the Student class and display the details of multiple students.
Q28. Write a Java program that defines a class Rectangle with attributes length and breadth. Implement two
constructors: one default constructor and one parameterized constructor. Use the constructors to initialize
the rectangle’s dimensions and calculate its area.
Q29. Write a Java class Employee that has the attributes name, salary, and department. Include a method to
display the employee’s details and another method to calculate an annual bonus based on the employee’s
salary.
Q30. Write a Java program that defines a class Car with attributes brand, model, and price. Use private access
for price, public for brand, and protected for model.