Computer Science, asked by anmol3247, 9 months ago

Write a java program to make a calculator . ?​

Answers

Answered by siddharthgager30
2

Answer:

The * operator entered by the user is stored in the operator variable using the next() method of Scanner object. Likewise, the two operands, 1.5 and 4.5 are stored in variables first and second respectively using the nextDouble() method of Scanner object.

Explanation:

I hope it's help you brother

like, rate an you can mark as brainliest!

Answered by Anonymous
1

Calculator - Java

We have to write a simple Java program to create a calculator using arithmetic operations: sum, difference, product, division and modulus.

We start by using the Scanner class to take user input for the two numbers. We store the numbers as double data type, which will help maintaining precision in the division. [Integer data type will not give an intended answer for the division operator. Also, it won't allow fractional number input.]

After that, we calculate the required quantities by performing the arithmetic operations using the +, -, *, /, // and % operators.

To perform each operation, we add an if-else block to perform an operation.

The Java Program Cōde

import java.util.Scanner;   // Importing Scanner

public class Calculator {   // Creating Class

   

   // The MAIN Function

   public static void main(String[] args) {

       

       // Create object of scanner

       Scanner sc = new Scanner(System.in);

       

       System.out.println("Below are the numbers signifying each operation. Choose what operation you'd like to perform.\n");

       System.out.println("Addition - 1\nSubtraction - 2\nMultiplication - 3\nDivision - 4\nModulus - 5\n");

       System.out.print("Enter the operation you'd like to perform [1/2/3/4/5]: ");

       

       // Take Input for choice

       int choice = sc.nextInt();

       

       if (choice == 1) {

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

           double a = sc.nextDouble();

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

           double b = sc.nextDouble();

           System.out.println("You've chosen addition. The resultant value is " + (a+b));

       }

       

       else if (choice == 2) {

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

           double a = sc.nextDouble();

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

           double b = sc.nextDouble();

           System.out.println("You've chosen subtraction. The resultant value is " + (a-b));

       }

       

       else if (choice == 3) {

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

           double a = sc.nextDouble();

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

           double b = sc.nextDouble();

           System.out.println("You've chosen multiplication. The resultant value is " + (a*b));

       }

       

       else if (choice == 4) {

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

           double a = sc.nextDouble();

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

           double b = sc.nextDouble();

           System.out.println("You've chosen divison. The resultant value is " + (a/b));

       }

       

       else if (choice == 5) {

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

           double a = sc.nextDouble();

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

           double b = sc.nextDouble();

           System.out.println("You've chosen floor divison. The resultant value is " + (a%b));

       }

       

       // Close Scanner

       sc.close();

       }

   }

Program Output

Here are two different runs of the program:

  • First run

Below are the numbers signifying each operation. Choose what operation you'd like to perform.

Addition - 1

Subtraction - 2

Multiplication - 3

Division - 4

Modulus - 5

Enter the operation you'd like to perform [1/2/3/4/5]: 1

Enter the first number: 25

Enter the second number: 25

You've chosen addition. The resultant value is 50.0

  • Second run

Below are the numbers signifying each operation. Choose what operation you'd like to perform.

Addition - 1

Subtraction - 2

Multiplication - 3

Division - 4

Modulus - 5

Enter the operation you'd like to perform [1/2/3/4/5]: 2

Enter the first number: 23

Enter the second number: 3

You've chosen subtraction. The resultant value is 20.0

\rule{300}{2}

Screenshots of the Java Program Cōde and the Output Runs have been attached.

Attachments:
Similar questions