Sr. No. Practical Page no.
1. Write a program to 2-3
perform following
operations on two
numbers input by the
user:
1) Addition
2) Subtraction
3)multiplication
4)division
2. Write a program to 3
convert temperature
from Fahrenheit to
Celsius degree using
Java.
3. Find the index of an 3-4
array element by writing
a program in Java
4. Write a Java program 5
that prints current time
in GMT.
5. Write a Java program to 5-6
remove a specific
element from an array
6. To write and read a plain 6-8
text file, write a Java
program.
7. Design a program to 8-9
copy an array by
iterating the array.
8. Compute the average of 9-10
three numbers through
a Java Program.
Aim:- Write a program to perform following operations on two numbers input by the user:
1) Addition
2) Subtraction
3) multiplication
4) division
Solution:-
Here’s a simple Java program that performs the specified operations on two numbers input by the user:
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input first number
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
// Input second number
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
// Addition
double sum = num1 + num2;
System.out.println("Sum: " + sum);
// Subtraction
double difference = num1 - num2;
System.out.println("Difference: " + difference);
// Multiplication
double product = num1 * num2;
System.out.println("Product: " + product);
// Division
if (num2 != 0) {
double quotient = num1 / num2;
System.out.println("Quotient: " + quotient);
} else {
System.out.println("Cannot divide by zero.");
scanner.close();
Aim:- Write a program to convert temperature from Fahrenheit to Celsius degree using Java.
Solution:-
Here’s a simple Java program that converts temperature from Fahrenheit to Celsius:
Import java.util.Scanner;
Public class FahrenheitToCelsiusConverter {
Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input temperature in Fahrenheit
System.out.print(“Enter temperature in Fahrenheit: “);
Double fahrenheit = scanner.nextDouble();
// Convert Fahrenheit to Celsius
Double celsius = (fahrenheit – 32) * 5 / 9;
// Display the result
System.out.println(“Temperature in Celsius: “ + celsius);
Scanner.close();
Aim:-Find the index of an array element by writing a program in Java
Solution:-
Here’s a simple Java program that finds the index of an element in an array:
Import java.util.Scanner;
Public class ArrayElementIndexFinder {
Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Sample array
Int[] array = {10, 20, 30, 40, 50};
// Input element to find
System.out.print(“Enter the element to find: “);
Int elementToFind = scanner.nextInt();
// Find the index of the element
Int index = -1;
For (int I = 0; I < array.length; i++) {
If (array[i] == elementToFind) {
Index = I;
Break; // Exit the loop once the element is found
// Display the result
If (index != -1) {
System.out.println(“Element “ + elementToFind + “ found at index “ + index);
} else {
System.out.println(“Element “ + elementToFind + “ not found in the array”);
Scanner.close();
}
Aim:-Write a Java program that prints current time in GMT.
Solution:-
You can use the java.time package to get the current time in GMT. Here’s a simple Java program:
Import java.time.ZoneId;
Import java.time.ZonedDateTime;
Import java.time.format.DateTimeFormatter;
Public class CurrentTimeInGMT {
Public static void main(String[] args) {
// Get the current time in GMT
ZonedDateTime gmtTime = ZonedDateTime.now(ZoneId.of(“GMT”));
// Format the time for display
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“HH:mm:ss”);
// Display the result
System.out.println(“Current Time in GMT: “ + gmtTime.format(formatter));
Aim:-Write a Java program to remove a specific element from an array
Solution:-
Here’s a simple Java program that removes a specific element from an array:
Import java.util.Arrays;
Public class RemoveElementFromArray {
Public static void main(String[] args) {
// Sample array
Int[] array = {10, 20, 30, 40, 50};
// Element to remove
Int elementToRemove = 30;
// Remove the element
Int indexToRemove = -1;
For (int I = 0; I < array.length; i++) {
If (array[i] == elementToRemove) {
indexToRemove = I;
break;
If (indexToRemove != -1) {
// Create a new array without the element
Int[] newArray = new int[array.length – 1];
System.arraycopy(array, 0, newArray, 0, indexToRemove);
System.arraycopy(array, indexToRemove + 1, newArray, indexToRemove, array.length –
indexToRemove – 1);
// Display the result
System.out.println(“Array after removing element “ + elementToRemove + “: “ +
Arrays.toString(newArray));
} else {
System.out.println(“Element “ + elementToRemove + “ not found in the array”);
Aim:-To write and read a plain text file, write a Java program.
Solution:-
Below is a simple Java program that demonstrates writing and reading a plain text file:
Import java.io.BufferedReader;
Import java.io.BufferedWriter;
Import java.io.FileReader;
Import java.io.FileWriter;
Import java.io.IOException;
Public class TextFileReadWrite {
Public static void main(String[] args) {
// Write to a text file
writeToFile(“sample.txt”, “Hello, this is a text file!”);
// Read from the same text file
String content = readFromFile(“sample.txt”);
// Display the content read from the file
System.out.println(“Content read from the file: \n” + content);
Private static void writeToFile(String filename, String content) {
Try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
Writer.write(content);
System.out.println(“Content written to the file successfully.”);
} catch (IOException e) {
System.err.println(“Error writing to the file: “ + e.getMessage());
Private static String readFromFile(String filename) {
StringBuilder content = new StringBuilder();
Try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
While ((line = reader.readLine()) != null) {
Content.append(line).append(“\n”);
} catch (IOException e) {
System.err.println(“Error reading from the file: “ + e.getMessage());
}
Return content.toString();
Aim:-Design a program to copy an array by iterating the array.
Solution:- Below is a simple Java program that copies an array by iterating through each
element:
public class ArrayCopy {
public static void main(String[] args) {
// Sample array
int[] originalArray = {1, 2, 3, 4, 5};
// Copy the array
int[] copiedArray = copyArray(originalArray);
// Display the original and copied arrays
System.out.println("Original Array: " + arrayToString(originalArray));
System.out.println("Copied Array: " + arrayToString(copiedArray));
private static int[] copyArray(int[] original) {
// Create a new array with the same length as the original
int[] copy = new int[original.length];
// Iterate through each element and copy it
for (int i = 0; i < original.length; i++) {
copy[i] = original[i];
return copy;
private static String arrayToString(int[] array) {
// Convert array to string for display
StringBuilder result = new StringBuilder("[");
for (int i = 0; i < array.length; i++) {
result.append(array[i]);
if (i < array.length - 1) {
result.append(", ");
result.append("]");
return result.toString();
Aim:-Compute the average of three numbers through a Java Program.
Solution:-Here’s a simple Java program that computes the average of three numbers:
import java.util.Scanner;
public class AverageCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input three numbers
System.out.print("Enter the first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter the second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter the third number: ");
double num3 = scanner.nextDouble();
// Calculate the average
double average = (num1 + num2 + num3) / 3;
// Display the result
System.out.println("Average of the three numbers: " + average);
scanner.close();