Bca 3rd year java ass.
Bca 3rd year java ass.
System.out.println("int: " + num + ", float: " + fNum + ", double: " + dNum);
Output
Q2. Write a program to demonstrate explicit type casting from double to int and observe the data loss.
double d = 123.45;
int i = (int) d;
Output
Q3. Write a program to accept two numbers from the user and perform all arithmetic operations (+, -, *,
/, %).
import java.util.Scanner;
int a = sc.nextInt();
int b = sc.nextInt();
Output
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3
Modulus: 1
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;
public class GreatestNumber {
Output
Greatest: 20
Q5. Write a Java program that demonstrates both pre-increment and post-increment behavior.
int x = 5;
Output
Post-increment: 5
After post-increment: 6
Pre-increment: 7
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;
if (num % 2 == 0)
else
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;
public class VotingEligibility {
System.out.println("Eligible to vote");
System.out.println(age >= 18 && age < 21 ? "Adult but young voter" : "Mature voter");
} else {
Output
Enter age: 16
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;
switch (day) {
Output
Wednesday
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
Enter marks: 85
Grade: B
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;
String result = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? "Leap year" : "Not a leap
year";
System.out.println(result);
}
}
Output
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;
char op = sc.next().charAt(0);
switch (op) {
Output
Enter two numbers: 12 4
Result: 48
Q12. Write a Java program to find the sum of the first n natural numbers using a for loop.
import java.util.Scanner;
System.out.print("Enter n: ");
Output
Enter n: 5
Sum: 15
Q13. Write a Java program to calculate the factorial of a number using a while loop.
import java.util.Scanner;
int n = sc.nextInt();
int fact = 1, i = 1;
Output
Enter a number: 5
Factorial: 120
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;
int n = sc.nextInt();
if (n % i == 0) {
isPrime = false;
break;
System.out.println(n + " is " + (isPrime ? "a prime number" : "not a prime number"));
Output
Enter a number: 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;
System.out.print("Enter n: ");
int next = a + b;
a = b;
b = next;
count++;
}
}
Output
Enter n: 5
01123
Q16. Write a program to reverse the digits of a number using a while loop.
import java.util.Scanner;
while (num != 0) {
num /= 10;
Output
Reversed: 4321
Q17. Write a Java program to print the multiplication table of a given number using a for loop.
import java.util.Scanner;
Output
Enter a number: 5
5x1=5
5 x 2 = 10
...
5 x 10 = 50
Q18. Write a Java program using nested loops to print the following pattern: 1 1 2 1 2 3 1 2 3 4
System.out.println();
Output
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.
import java.util.Scanner;
do {
num /= 10;
Output
Sum of digits: 6
Q20. Write a Java program to check if a number is a palindrome or not using a while loop.
import java.util.Scanner;
num /= 10;
Output
121 is a palindrome
Q21. Write a Java program to count the number of vowels in a given string using a for loop.
import java.util.Scanner;
int count = 0;
Output
Number of vowels: 3
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.
System.out.println(i);
Output
...
Q23. Write a Java program that prints numbers from 1 to 10, but uses the continue statement to skip
printing the number 4.
if (i == 4) continue;
System.out.println(i);
Output
3
5
...
10
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.
import java.util.Scanner;
switch (month) {
Output
March
Q25. Write a Java program that defines a method to find the maximum of two numbers using the return
import java.util.Scanner;
return (a > b) ? a : b;
Output
Enter two numbers: 5 8
Maximum: 8
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.
if (i == 8) break;
if (i == 5) continue;
System.out.println(i);
Output
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.
import java.util.Scanner;
class Student {
String name;
int rollNumber;
float marks;
void inputDetails() {
name = sc.nextLine();
rollNumber = sc.nextInt();
marks = sc.nextFloat();
void displayDetails() {
s1.inputDetails();
s1.displayDetails();
Output
Name: John
Marks: 85.5
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.
class Rectangle {
length = 0;
breadth = 0;
this.length = length;
this.breadth = breadth;
}
int calculateArea() {
Output
Default Area: 0
Parameterized Area: 50
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.
class Employee {
this.name = name;
this.department = department;
this.salary = salary;
void displayDetails() {
double calculateBonus() {
emp.displayDetails();
}
}
Output
Name: Alice
Department: HR
Salary: 50000.0
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.
class Car {
this.brand = brand;
this.model = model;
this.price = price;
void displayDetails() {
}
}
car.displayDetails();
Output
Brand: Toyota
Model: Corolla