Java Programming Lab-I Solutions
21 days ago
Java Programming Lab-I Solutions
Here are the solutions for all 25 programming problems:
1. Display "Hello BCA"
public class HelloBCA {
public static void main(String[] args) {
System.out.println("Hello BCA");
}
}
Output:
Copy
Download
Hello BCA
2. Display square of a number
java
Copy
Download
import java.util.Scanner;
public class SquareNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
System.out.println("Square of " + num + " is " + (num * num));
sc.close();
}
}
Sample Output:
Copy
Download
Enter a number: 5
Square of 5 is 25
3. Sum, difference and product of two numbers
java
Copy
Download
import java.util.Scanner;
public class ArithmeticOperations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
System.out.println("Sum: " + (num1 + num2));
System.out.println("Difference: " + (num1 - num2));
System.out.println("Product: " + (num1 * num2));
sc.close();
}
}
Sample Output:
Copy
Download
Enter first number: 8
Enter second number: 3
Sum: 11
Difference: 5
Product: 24
4. Calculate area and circumference of circle
java
Copy
Download
import java.util.Scanner;
public class CircleCalculations {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius of circle: ");
double radius = sc.nextDouble();
double area = Math.PI * radius * radius;
double circumference = 2 * Math.PI * radius;
System.out.printf("Area: %.2f\n", area);
System.out.printf("Circumference: %.2f\n", circumference);
sc.close();
}
}
Sample Output:
Copy
Download
Enter radius of circle: 5
Area: 78.54
Circumference: 31.42
5. Area between two concentric circles
java
Copy
Download
import java.util.Scanner;
public class ConcentricCircles {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius of larger circle: ");
double r1 = sc.nextDouble();
System.out.print("Enter radius of smaller circle: ");
double r2 = sc.nextDouble();
double area = Math.PI * (r1 * r1 - r2 * r2);
System.out.printf("Area between circles: %.2f\n", area);
sc.close();
}
}
Sample Output:
Copy
Download
Enter radius of larger circle: 8
Enter radius of smaller circle: 5
Area between circles: 122.52
6. Convert kilometers to meters
java
Copy
Download
import java.util.Scanner;
public class KilometerToMeter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter length in kilometers: ");
double km = sc.nextDouble();
double meters = km * 1000;
System.out.println(km + " km = " + meters + " meters");
sc.close();
}
}
Sample Output:
Copy
Download
Enter length in kilometers: 2.5
2.5 km = 2500.0 meters
7. Calculate simple interest and amount
java
Copy
Download
import java.util.Scanner;
public class SimpleInterest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter principal amount: ");
double principal = sc.nextDouble();
System.out.print("Enter rate of interest: ");
double rate = sc.nextDouble();
System.out.print("Enter time in years: ");
double time = sc.nextDouble();
double interest = (principal * rate * time) / 100;
double amount = principal + interest;
System.out.println("Simple Interest: " + interest);
System.out.println("Amount: " + amount);
sc.close();
}
}
Sample Output:
Copy
Download
Enter principal amount: 10000
Enter rate of interest: 5
Enter time in years: 2
Simple Interest: 1000.0
Amount: 11000.0
8. Area of rectangle
java
Copy
Download
import java.util.Scanner;
public class RectangleArea {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter length of rectangle: ");
double length = sc.nextDouble();
System.out.print("Enter width of rectangle: ");
double width = sc.nextDouble();
double area = length * width;
System.out.println("Area of rectangle: " + area);
sc.close();
}
}
Sample Output:
Copy
Download
Enter length of rectangle: 10
Enter width of rectangle: 5
Area of rectangle: 50.0
9. Convert Celsius to Fahrenheit
java
Copy
Download
import java.util.Scanner;
public class TemperatureConverter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter temperature in Celsius: ");
double celsius = sc.nextDouble();
double fahrenheit = (9.0 / 5) * celsius + 32;
System.out.println(celsius + "°C = " + fahrenheit + "°F");
sc.close();
}
}
Sample Output:
Copy
Download
Enter temperature in Celsius: 37
37.0°C = 98.6°F
10. Swap two variables
java
Copy
Download
import java.util.Scanner;
public class SwapVariables {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
System.out.println("Before swapping: a = " + a + ", b = " + b);
// Swapping using temporary variable
int temp = a;
a = b;
b = temp;
System.out.println("After swapping: a = " + a + ", b = " + b);
sc.close();
}
}
Sample Output:
Copy
Download
Enter first number: 10
Enter second number: 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10
11. Calculate student marks
java
Copy
Download
import java.util.Scanner;
public class StudentMarks {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter marks of 5 subjects (out of 100):");
int total = 0;
for (int i = 1; i <= 5; i++) {
System.out.print("Subject " + i + ": ");
int marks = sc.nextInt();
total += marks;
}
double percentage = (double) total / 5;
String result = (percentage >= 40) ? "Pass" : "Fail";
System.out.println("Total Marks: " + total);
System.out.printf("Percentage: %.2f%%\n", percentage);
System.out.println("Result: " + result);
sc.close();
}
}
Sample Output:
Copy
Download
Enter marks of 5 subjects (out of 100):
Subject 1: 85
Subject 2: 76
Subject 3: 92
Subject 4: 88
Subject 5: 80
Total Marks: 421
Percentage: 84.20%
Result: Pass
12. Convert days to months and days
java
Copy
Download
import java.util.Scanner;
public class DaysConverter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of days: ");
int totalDays = sc.nextInt();
int months = totalDays / 30;
int days = totalDays % 30;
System.out.println(totalDays + " days = " + months + " months and " + days + " days");
sc.close();
}
}
Sample Output:
Copy
Download
Enter number of days: 75
75 days = 2 months and 15 days
13. Convert seconds to hours, minutes, seconds
java
Copy
Download
import java.util.Scanner;
public class TimeConverter {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter time in seconds: ");
int totalSeconds = sc.nextInt();
int hours = totalSeconds / 3600;
int remainingSeconds = totalSeconds % 3600;
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
System.out.printf("%d seconds = %d hours, %d minutes, %d seconds\n",
totalSeconds, hours, minutes, seconds);
sc.close();
}
}
Sample Output:
Copy
Download
Enter time in seconds: 4520
4520 seconds = 1 hours, 15 minutes, 20 seconds
14. Calculate age from birth date
java
Copy
Download
import java.util.Scanner;
public class AgeCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input present date
System.out.println("Enter present date:");
System.out.print("Year: ");
int presentYear = sc.nextInt();
System.out.print("Month (1-12): ");
int presentMonth = sc.nextInt();
System.out.print("Day: ");
int presentDay = sc.nextInt();
// Input birth date
System.out.println("\nEnter birth date:");
System.out.print("Year: ");
int birthYear = sc.nextInt();
System.out.print("Month (1-12): ");
int birthMonth = sc.nextInt();
System.out.print("Day: ");
int birthDay = sc.nextInt();
// Calculate age
int years = presentYear - birthYear;
int months = presentMonth - birthMonth;
int days = presentDay - birthDay;
if (days < 0) {
months--;
days += 30; // Approximation
}
if (months < 0) {
years--;
months += 12;
}
System.out.printf("\nYour age is: %d years, %d months, %d days\n", years, months, days);
sc.close();
}
}
Sample Output:
Copy
Download
Enter present date:
Year: 2023
Month (1-12): 10
Day: 15
Enter birth date:
Year: 1990
Month (1-12): 5
Day: 20
Your age is: 33 years, 4 months, 25 days
15. Check positive or negative number
java
Copy
Download
import java.util.Scanner;
public class PositiveNegative {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
double num = sc.nextDouble();
if (num > 0) {
System.out.println(num + " is positive");
} else if (num < 0) {
System.out.println(num + " is negative");
} else {
System.out.println(num + " is zero");
}
sc.close();
}
}
Sample Output:
Copy
Download
Enter a number: -5
-5.0 is negative
16. Check even or odd number
java
Copy
Download
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");
}
sc.close();
}
}
Sample Output:
Copy
Download
Enter a number: 7
7 is odd
17. Calculate profit or loss
java
Copy
Download
import java.util.Scanner;
public class ProfitLoss {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Cost Price: ");
double cp = sc.nextDouble();
System.out.print("Enter Selling Price: ");
double sp = sc.nextDouble();
if (sp > cp) {
double profit = sp - cp;
System.out.println("Profit: " + profit);
} else if (cp > sp) {
double loss = cp - sp;
System.out.println("Loss: " + loss);
} else {
System.out.println("No profit, no loss");
}
sc.close();
}
}
Sample Output:
Copy
Download
Enter Cost Price: 500
Enter Selling Price: 600
Profit: 100.0
18. Find smallest of two numbers
java
Copy
Download
import java.util.Scanner;
public class SmallestOfTwo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
int smallest = (num1 < num2) ? num1 : num2;
System.out.println("Smallest number is: " + smallest);
sc.close();
}
}
Sample Output:
Copy
Download
Enter first number: 25
Enter second number: 18
Smallest number is: 18
19. Find largest of three numbers
java
Copy
Download
import java.util.Scanner;
public class LargestOfThree {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
System.out.print("Enter third number: ");
int num3 = sc.nextInt();
int largest = num1;
if (num2 > largest) largest = num2;
if (num3 > largest) largest = num3;
System.out.println("Largest number is: " + largest);
sc.close();
}
}
Sample Output:
Copy
Download
Enter first number: 12
Enter second number: 25
Enter third number: 8
Largest number is: 25
20. Find smallest of four numbers
java
Copy
Download
import java.util.Scanner;
public class SmallestOfFour {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
System.out.print("Enter third number: ");
int num3 = sc.nextInt();
System.out.print("Enter fourth number: ");
int num4 = sc.nextInt();
int smallest = num1;
if (num2 < smallest) smallest = num2;
if (num3 < smallest) smallest = num3;
if (num4 < smallest) smallest = num4;
System.out.println("Smallest number is: " + smallest);
sc.close();
}
}
Sample Output:
Copy
Download
Enter first number: 15
Enter second number: 8
Enter third number: 22
Enter fourth number: 6
Smallest number is: 6
21. Display natural numbers up to 100
java
Copy
Download
public class NaturalNumbers {
public static void main(String[] args) {
System.out.println("Natural numbers up to 100:");
for (int i = 1; i <= 100; i++) {
System.out.print(i + " ");
if (i % 10 == 0) System.out.println(); // New line every 10 numbers
}
}
}
Sample Output (partial):
Copy
Download
Natural numbers up to 100:
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
...
91 92 93 94 95 96 97 98 99 100
22. Display even numbers up to n
java
Copy
Download
import java.util.Scanner;
public class EvenNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number (n): ");
int n = sc.nextInt();
System.out.println("Even numbers up to " + n + ":");
for (int i = 2; i <= n; i += 2) {
System.out.print(i + " ");
if (i % 20 == 0) System.out.println(); // New line every 10 even numbers
}
sc.close();
}
}
Sample Output:
Copy
Download
Enter a number (n): 20
Even numbers up to 20:
2 4 6 8 10 12 14 16 18 20
23. Sum of natural numbers up to 50
java
Copy
Download
public class SumNaturalNumbers {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 50; i++) {
sum += i;
}
System.out.println("Sum of natural numbers up to 50: " + sum);
}
}
Output:
Copy
Download
Sum of natural numbers up to 50: 1275
24. Product of odd numbers up to n
java
Copy
Download
import java.util.Scanner;
public class ProductOddNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number (n): ");
int n = sc.nextInt();
long product = 1;
for (int i = 1; i <= n; i += 2) {
product *= i;
}
System.out.println("Product of odd numbers up to " + n + ": " + product);
sc.close();
}
}
Sample Output:
Copy
Download
Enter a number (n): 7
Product of odd numbers up to 7: 105
25. Calculate factorial of a number
java
Copy
Download
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();
long factorial = 1;
for (int i = 1; i <= n; i++) {
factorial *= i;
}
System.out.println("Factorial of " + n + ": " + factorial);
sc.close();
}
}
Sample Output:
Copy
Download
Enter a number: 5
Factorial of 5: 120
Each program includes comments and follows good coding practices. The programs demonstrate basic Java concepts including input/output, arithmetic operations, conditional statements, loops, and more.