Computer Science, asked by ismandal70, 9 months ago

java program for a calculator​

Answers

Answered by Falcon06
1

import java.util.Scanner;

public class Main {

static int sum(int a, int b){

return a+b;

}

public static void main(String[] args) {

System.out.println("Made by Dhrubo");

float number_1, number_2;

System.out.println("Enter first number");

Scanner scan = new Scanner(System.in);

number_1 = scan.nextFloat();

System.out.println("Enter second number");

number_2 = scan.nextFloat();

System.out.print("You have Entered ");

System.out.print(number_1);

System.out.print(" and ");

System.out.println(number_2);

String prompt = "Enter 0 for addition, 1 for subtraction, 2 for multiplication and 3 for division";

System.out.println(prompt);{}

int input = scan.nextInt();

switch (input){

case 0:

System.out.println("Adding these numbers");

System.out.print("The result is: ");

System.out.println(number_1 + number_2);

break;

case 1:

System.out.println("Subtracting these numbers");

System.out.print("The result is: ");

System.out.println(number_1 - number_2);

break;

case 2:

System.out.println("Multiplying these numbers");

System.out.print("The result is: ");

System.out.println(number_1 * number_2);

break;

case 3:

System.out.println("Dividing these numbers");{}

System.out.print("The result is: ");

System.out.println(number_1 / number_2);

break;

default:

System.out.println("Invalid input");

}

}

}

Answered by Anshu33845
0

Answer:

This is a program of calculator in Java using switch statement.

Explanation:

import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

System.out.print("Enter two numbers: ");

double first = reader.nextDouble();

double second = reader.nextDouble();

System.out.print("Enter an operator (+, -, *, /): ");

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

double result;

switch(operator)

{

case '+':

result = first + second;

break;

case '-':

result = first - second;

break;

case '*':

result = first * second;

break;

case '/':

result = first / second;

break;

default:

System.out.printf("Error! Invalid typing dear.");

return;

}

System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);

}

}

Similar questions