0% found this document useful (0 votes)
37 views26 pages

Bca 3rd year java ass.

Uploaded by

Siddarth Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views26 pages

Bca 3rd year java ass.

Uploaded by

Siddarth Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Q1. Write a Java program to demonstrate automatic type conversion from int to float and double.

public class AutoTypeConversion {

public static void main(String[] args) {

int num = 100;

float fNum = num;

double dNum = num;

System.out.println("int: " + num + ", float: " + fNum + ", double: " + dNum);

Output

int: 100, float: 100.0, double: 100.0

Q2. Write a program to demonstrate explicit type casting from double to int and observe the data loss.

public class ExplicitCasting {

public static void main(String[] args) {

double d = 123.45;

int i = (int) d;

System.out.println("Double: " + d + ", Casted int: " + i);

Output

Double: 123.45, Casted int: 123

Q3. Write a program to accept two numbers from the user and perform all arithmetic operations (+, -, *,
/, %).
import java.util.Scanner;

public class ArithmeticOperations {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter two numbers: ");

int a = sc.nextInt();

int b = sc.nextInt();

System.out.println("Addition: " + (a + b));

System.out.println("Subtraction: " + (a - b));

System.out.println("Multiplication: " + (a * b));

System.out.println("Division: " + (a / b));

System.out.println("Modulus: " + (a % b));

Output

Enter two numbers: 10 3

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 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter three numbers: ");

int a = sc.nextInt(), b = sc.nextInt(), c = sc.nextInt();

int max = (a > b && a > c) ? a : (b > c ? b : c);

System.out.println("Greatest: " + max);

Output

Enter three numbers: 10 20 15

Greatest: 20

Q5. Write a Java program that demonstrates both pre-increment and post-increment behavior.

public class IncrementDemo {

public static void main(String[] args) {

int x = 5;

System.out.println("Post-increment: " + (x++));

System.out.println("After post-increment: " + x);

System.out.println("Pre-increment: " + (++x));

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;

public class EvenOdd {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

if (num % 2 == 0)

System.out.println(num + " is even");

else

System.out.println(num + " is odd");

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 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter age: ");

int age = sc.nextInt();

if (age >= 18) {

System.out.println("Eligible to vote");

System.out.println(age >= 18 && age < 21 ? "Adult but young voter" : "Mature voter");

} else {

System.out.println("Not eligible (Minor)");

Output

Enter age: 16

Not eligible (Minor)

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;

public class DayOfWeek {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number (1-7): ");


int day = sc.nextInt();

switch (day) {

case 1 -> System.out.println("Monday");

case 2 -> System.out.println("Tuesday");

case 3 -> System.out.println("Wednesday");

case 4 -> System.out.println("Thursday");

case 5 -> System.out.println("Friday");

case 6 -> System.out.println("Saturday");

case 7 -> System.out.println("Sunday");

default -> System.out.println("Invalid input");

Output

Enter a number (1-7): 3

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;

public class GradeAssignment {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


System.out.print("Enter marks: ");

int marks = sc.nextInt();

if (marks >= 90) System.out.println("Grade: A");

else if (marks >= 80) System.out.println("Grade: B");

else if (marks >= 70) System.out.println("Grade: C");

else if (marks >= 60) System.out.println("Grade: D");

else System.out.println("Grade: F");

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;

public class LeapYear {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a year: ");

int year = sc.nextInt();

String result = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? "Leap year" : "Not a leap
year";

System.out.println(result);

}
}

Output

Enter a year: 2024

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;

public class ArithmeticSwitch {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter two numbers: ");

int a = sc.nextInt(), b = sc.nextInt();

System.out.print("Enter operator (+, -, *, /): ");

char op = sc.next().charAt(0);

switch (op) {

case '+' -> System.out.println("Result: " + (a + b));

case '-' -> System.out.println("Result: " + (a - b));

case '*' -> System.out.println("Result: " + (a * b));

case '/' -> System.out.println("Result: " + (a / b));

default -> System.out.println("Invalid operator");

Output
Enter two numbers: 12 4

Enter operator (+, -, *, /): *

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;

public class SumNaturalNumbers {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter n: ");

int n = sc.nextInt(), sum = 0;

for (int i = 1; i <= n; i++) sum += i;

System.out.println("Sum: " + sum);

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;

public class Factorial {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

int fact = 1, i = 1;

while (i <= n) fact *= i++;

System.out.println("Factorial: " + fact);

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;

public class PrimeCheck {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int n = sc.nextInt();

boolean isPrime = n > 1;

for (int i = 2; i <= Math.sqrt(n); i++) {

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;

public class Fibonacci {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter n: ");

int n = sc.nextInt(), a = 0, b = 1, count = 0;

while (count < n) {

System.out.print(a + " ");

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;

public class ReverseNumber {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt(), reversed = 0;

while (num != 0) {

reversed = reversed * 10 + num % 10;

num /= 10;

System.out.println("Reversed: " + reversed);

Output

Enter a number: 1234

Reversed: 4321
Q17. Write a Java program to print the multiplication table of a given number using a for loop.

import java.util.Scanner;

public class MultiplicationTable {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt();

for (int i = 1; i <= 10; i++) {

System.out.println(num + " x " + i + " = " + (num * i));

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

public class Pattern {

public static void main(String[] args) {

for (int i = 1; i <= 4; i++) {


for (int j = 1; j <= i; j++) {

System.out.print(j + " ");

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;

public class SumOfDigits {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt(), sum = 0;

do {

sum += num % 10;

num /= 10;

} while (num > 0);


System.out.println("Sum of digits: " + sum);

Output

Enter a number: 123

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;

public class Palindrome {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number: ");

int num = sc.nextInt(), reversed = 0, original = num;

while (num > 0) {

reversed = reversed * 10 + num % 10;

num /= 10;

System.out.println(original + (original == reversed ? " is a palindrome" : " is not a palindrome"));

Output

Enter a number: 121

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;

public class VowelCount {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a string: ");

String str = sc.nextLine().toLowerCase();

int count = 0;

for (char ch : str.toCharArray()) {

if ("aeiou".indexOf(ch) != -1) count++;

System.out.println("Number of vowels: " + count);

Output

Enter a string: Hello World

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.

public class BreakDemo {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {


if (i == 7) break;

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.

public class ContinueDemo {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

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;

public class MonthSwitch {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a month number: ");

int month = sc.nextInt();

switch (month) {

case 1 -> System.out.println("January");

case 2 -> System.out.println("February");

case 3 -> System.out.println("March");

case 4 -> System.out.println("April");

case 5 -> System.out.println("May");

case 6 -> System.out.println("June");

case 7 -> System.out.println("July");

case 8 -> System.out.println("August");

case 9 -> System.out.println("September");

case 10 -> System.out.println("October");

case 11 -> System.out.println("November");

case 12 -> System.out.println("December");


default -> System.out.println("Invalid month number");

Output

Enter a month number: 3

March

Q25. Write a Java program that defines a method to find the maximum of two numbers using the return

import java.util.Scanner;

public class MaxNumber {

public static int findMax(int a, int b) {

return (a > b) ? a : b;

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter two numbers: ");

int a = sc.nextInt(), b = sc.nextInt();

System.out.println("Maximum: " + findMax(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.

public class BreakAndContinue {

public static void main(String[] args) {

for (int i = 1; i <= 10; i++) {

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() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter name: ");

name = sc.nextLine();

System.out.print("Enter roll number: ");

rollNumber = sc.nextInt();

System.out.print("Enter marks: ");

marks = sc.nextFloat();

void displayDetails() {

System.out.println("Name: " + name);

System.out.println("Roll Number: " + rollNumber);

System.out.println("Marks: " + marks);

public class StudentDemo {

public static void main(String[] args) {


Student s1 = new Student();

s1.inputDetails();

s1.displayDetails();

Output

Enter name: John

Enter roll number: 101

Enter marks: 85.5

Name: John

Roll Number: 101

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 {

int length, breadth;

Rectangle() { // Default constructor

length = 0;

breadth = 0;

Rectangle(int length, int breadth) { // Parameterized constructor

this.length = length;

this.breadth = breadth;
}

int calculateArea() {

return length * breadth;

public class RectangleDemo {

public static void main(String[] args) {

Rectangle r1 = new Rectangle(); // Using default constructor

System.out.println("Default Area: " + r1.calculateArea());

Rectangle r2 = new Rectangle(5, 10); // Using parameterized constructor

System.out.println("Parameterized Area: " + r2.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 {

String name, department;


double salary;

Employee(String name, String department, double salary) {

this.name = name;

this.department = department;

this.salary = salary;

void displayDetails() {

System.out.println("Name: " + name);

System.out.println("Department: " + department);

System.out.println("Salary: " + salary);

double calculateBonus() {

return salary * 0.10; // 10% bonus

public class EmployeeDemo {

public static void main(String[] args) {

Employee emp = new Employee("Alice", "HR", 50000);

emp.displayDetails();

System.out.println("Annual Bonus: " + emp.calculateBonus());

}
}

Output

Name: Alice

Department: HR

Salary: 50000.0

Annual Bonus: 5000.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 {

public String brand;

protected String model;

private double price;

Car(String brand, String model, double price) {

this.brand = brand;

this.model = model;

this.price = price;

void displayDetails() {

System.out.println("Brand: " + brand);

System.out.println("Model: " + model);

System.out.println("Price: " + price);

}
}

public class CarDemo {

public static void main(String[] args) {

Car car = new Car("Toyota", "Corolla", 20000);

car.displayDetails();

Output

Brand: Toyota

Model: Corolla

Price: 20000.0 sss

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy