0% found this document useful (0 votes)
15 views

Expt 6 Java

Java experiment

Uploaded by

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

Expt 6 Java

Java experiment

Uploaded by

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

import java.util.

Scanner;

public class BasicCalculator {

public static void main(String[] args) {

// Create a Scanner object to take input from the user

Scanner input = new Scanner(System.in);

// Ask the user to enter two numbers

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

double num1 = input.nextDouble();

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

double num2 = input.nextDouble();

// Ask the user to choose an operation

System.out.println("Choose an operation: +, -, *, /");

char operator = input.next().charAt(0);

double result;

// Switch statement to perform the operation based on the user's choice

switch (operator) {

case '+':

result = num1 + num2;

System.out.println("Result: " + num1 + " + " + num2 + " = " + result);

break;

case '-':

result = num1 - num2;

System.out.println("Result: " + num1 + " - " + num2 + " = " + result);


break;

case '*':

result = num1 * num2;

System.out.println("Result: " + num1 + " * " + num2 + " = " + result);

break;

case '/':

if (num2 != 0) {

result = num1 / num2;

System.out.println("Result: " + num1 + " / " + num2 + " = " + result);

} else {

System.out.println("Error! Division by zero is not allowed.");

break;

default:

System.out.println("Invalid operator! Please choose +, -, *, or /.");

break;

// Close the scanner

input.close();

You might also like