Java Practicals:-
Unit - 3: Fundamentals of Java Program
Program 1: Hello World
Question: Write a Java program to print "Hello, World!".
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
Output:
Hello, World!
Program 2: Sum of Two Numbers
Question: Write a Java program to calculate the sum of two numbers.
import java.util.Scanner;
public class SumOfTwoNumbers {
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 sum = num1 + num2;
System.out.println("Sum: " + sum);
}
}
Output:
Enter first number: 10
Enter second number: 20
Sum: 30
Program 3: Area of a Circle
Question: Write a Java program to calculate the area of a circle.
import java.util.Scanner;
public class AreaOfCircle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter radius: ");
double radius = sc.nextDouble();
double area = Math.PI * radius * radius;
System.out.println("Area: " + area);
Output:
Enter radius: 5
Area: 78.53981633974483
Program 4: Simple Calculator
Question: Write a Java program to create a simple calculator.
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Simple Calculator");
System.out.println("1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Multiplication");
System.out.println("4. Division");
System.out.print("Enter choice: ");
int choice = sc.nextInt();
System.out.print("Enter first number: ");
double num1 = sc.nextDouble();
System.out.print("Enter second number: ");
double num2 = sc.nextDouble();
double result = 0;
switch (choice) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
result = num1 / num2;
break;
default:
System.out.println("Invalid choice");
return;
System.out.println("Result: " + result);
Output:
Simple Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter choice: 1
Enter first number: 10
Enter second number: 20
Result: 30.0
Program 5: Student Marks
Question: Write a Java program to calculate the total marks and average marks of a student.
import java.util.Scanner;
public class StudentMarks {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter student name: ");
String name = sc.nextLine();
System.out.print("Enter marks in Math: ");
int math = sc.nextInt();
System.out.print("Enter marks in Science: ");
int science = sc.nextInt();
System.out.print("Enter marks in English: ");
int english = sc.nextInt();
int total = math + science + english;
double average = (double) total / 3;
System.out.println("Student Name: " + name);
System.out.println("Math: " + math);
System.out.println("Science: " + science);
System.out.println("English: " + english);
System.out.println("Total: " + total);
System.out.println("Average: " + average);
Output:
Enter student name: John
Enter marks in Math: 80
Enter marks in Science: 70
Enter marks in English: 90
Student Name: John
Math: 80
Science: 70
English: 90
Total: 240
Average: 80.0
Program 6: Simple Interest Calculator
Question: Write a Java program to calculate the simple interest.
import java.util.Scanner;
public class SimpleInterestCalculator {
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 (in %): ");
double rate = sc.nextDouble();
System.out.print("Enter time period (in years): ");
double time = sc.nextDouble();
double interest = (principal * rate * time) / 100;
System.out.println("Simple Interest: " + interest);
Output:
Enter principal amount: 10000
Enter rate of interest (in %): 5
Enter time period (in years): 2
Simple Interest: 1000.0
Program 7: Factorial of a Number
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 num = sc.nextInt();
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
System.out.println("Factorial: " + factorial);
Output:
Enter a number: 5
Factorial: 120
Program 8: Check Prime Number
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
boolean isPrime = true;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
if (isPrime) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
Output:
Enter a number: 23
23 is a prime number.
Program 9: Fibonacci Series
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int t1 = 0, t2 = 1;
System.out.print("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
Output:
Enter the number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
Program 10: Check Palindrome
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();
int reverse = 0;
int original = num;
while (num != 0) {
int remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
if (original == reverse) {
System.out.println(original + " is a palindrome.");
} else {
System.out.println(original + " is not a palindrome.");
Output:
Enter a number: 121
121 is a palindrome.
Program 11: Binary Search
import java.util.Scanner;
public class BinarySearch {
public static void main(String[] args) {
int[] array = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to search: ");
int target = sc.nextInt();
int result = binarySearch(array, target);
if (result == -1) {
System.out.println("Number not found in the array.");
} else {
System.out.println("Number found at index " + result);
}
public static int binarySearch(int[] array, int target) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (array[mid] == target) {
return mid;
} else if (array[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
return -1;
Output:
Enter a number to search: 23
Number found at index 5
Program 12: Bubble Sort
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
int[] array = {64, 34, 25, 12, 22, 11, 90};
System.out.println("Original array:");
printArray(array);
bubbleSort(array);
System.out.println("Sorted array:");
printArray(array);
public static void bubbleSort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// Swap array[j] and array[j+1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
System.out.println();
}
Output:
Original array:
64 34 25 12 22 11 90
Sorted array:
11 12 22 25 34 64 90
Program 15: Calculate the Area and Perimeter of a Rectangle
Question: Write a Java program to calculate the area and perimeter of a rectangle.
import java.util.Scanner;
public class Rectangle {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length of the rectangle: ");
double length = sc.nextDouble();
System.out.print("Enter the width of the rectangle: ");
double width = sc.nextDouble();
double area = length * width;
double perimeter = 2 * (length + width);
System.out.println("Area of the rectangle: " + area);
System.out.println("Perimeter of the rectangle: " + perimeter);
Output:
Enter the length of the rectangle: 10
Enter the width of the rectangle: 5
Area of the rectangle: 50.0
Perimeter of the rectangle: 30.0
Program 16: Check if a Number is a Perfect Number
Question: Write a Java program to check if a number is a perfect number.
import java.util.Scanner;
public class PerfectNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
int sum = 0;
for (int i = 1; i < num; i++) {
if (num % i == 0) {
sum += i;
if (sum == num) {
System.out.println(num + " is a perfect number.");
} else {
System.out.println(num + " is not a perfect number.");
}
Output:
Enter a number: 6
6 is a perfect number.
Program 17: Generate a Fibonacci Series
Question: Write a Java program to generate a Fibonacci series.
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int n = sc.nextInt();
int t1 = 0, t2 = 1;
System.out.print("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
System.out.print(t1 + " ");
int sum = t1 + t2;
t1 = t2;
t2 = sum;
}
Output:
Enter the number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
Program 18: Check if a String is a Palindrome
Question: Write a Java program to check if a string is a palindrome.
import java.util.Scanner;
public class PalindromeString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = sc.nextLine();
String reverse = "";
for (int i = str.length() - 1; i >= 0; i--) {
reverse += str.charAt(i);
if (str.equals(reverse)) {
System.out.println(str + " is a palindrome.");
} else {
System.out.println(str + " is not a palindrome.");
}
Output:
Enter a string: madam
madam is a palindrome.
Program 19: Calculate the GCD of Two Numbers
Question: Write a Java program to calculate the GCD of two numbers.
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = sc.nextInt();
System.out.print("Enter the second number: ");
int num2 = sc.nextInt();
int gcd = 1;
for (int i = 1; i <= num1 && i <= num2; i++) {
if (num1 % i == 0 && num2 % i == 0) {
gcd = i;
System.out.println("GCD of " + num1 + " and " + num2 + " is " + gcd);
}
Output:
Enter the first number: 48
Enter the second number: 18
GCD of 48 and 18 is 6