0% found this document useful (0 votes)
4 views2 pages

Java Program p3 With Output

java info

Uploaded by

vangujarakash
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)
4 views2 pages

Java Program p3 With Output

java info

Uploaded by

vangujarakash
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/ 2

Java Program (BufferedReader Example) with

Output

■ Java Program:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

class p3 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter which arithmetic operation you want to perform: ");


String operation = reader.readLine().toLowerCase();

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


int first_number = Integer.parseInt(reader.readLine());

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


int second_number = Integer.parseInt(reader.readLine());

int result = 0;

if (operation.contains("add")) {
result = first_number + second_number;
System.out.println("The result of addition is: " + result);
} else if (operation.contains("subtract")) {
result = first_number - second_number;
System.out.println("The result of subtraction is: " + result);
} else if (operation.contains("multiply")) {
result = first_number * second_number;
System.out.println("The result of multiplication is: " + result);
} else if (operation.contains("divide")) {
if (second_number != 0) {
result = first_number / second_number;
System.out.println("The result of division is: " + result);
} else {
System.out.println("Division by zero is not allowed.");
}
} else {
System.out.println("Invalid operation. Please enter add, subtract, multiply, or divide.");
}
}
}

■ Program Output:
--- Sample Outputs ---

1) Operation: add
Enter first number: 15
Enter second number: 5
Output → The result of addition is: 20

2) Operation: subtract
Enter first number: 25
Enter second number: 10
Output → The result of subtraction is: 15

3) Operation: multiply
Enter first number: 6
Enter second number: 7
Output → The result of multiplication is: 42

4) Operation: divide
Enter first number: 40
Enter second number: 8
Output → The result of division is: 5

5) Operation: divide
Enter first number: 12
Enter second number: 0
Output → Division by zero is not allowed.

6) Operation: random
Enter first number: 5
Enter second number: 3
Output → Invalid operation. Please enter add, subtract, multiply, or divide.

You might also like