Computer Science, asked by gytgf6747, 1 year ago

Menu driven program for calculator in java

Answers

Answered by abhinav115
2
import java.util.*;
class calculator
{
public static void main(String Args[])
{
int a;double n1,n2;
double f;
System.out.println("*********************Calculator*********************");
System.out.println("\n\n\n1. Addition\n2. Subtraction\n3. Division\n4. Multiplication\n5. Exit");
System.out.print("\nEnter the serial no. of the operation you would like to perform : ");
Scanner ob=new Scanner(System.in);
a=ob.nextInt();
switch(a)
{
case 1:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1+n2;
    System.out.println("Final Answer is : "+f);
    break;
case 2:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1-n2;
    System.out.print("Final Answer is : "+f);
    break;
case 4:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1*n2;
    System.out.print("The product is : "+f);
    break;
case 3:
    System.out.print("\n\nEnter the first no. : ");
    n1=ob.nextInt();
    System.out.print("Enter the second no. : ");
    n2=ob.nextInt();
    f=n1/n2;
    System.out.print("The Quotient is : "+f);
    break;
case 5:
    break;
default:
    System.out.println("Enter the correct no.");
    break;
}
}
}
Answered by Shivu516
0

Hope it helps ^_^

This is my most perfect answer till now

This program is in Java

I have also added a variable descriptive table in the attached file

___________________________________

import java.util.Scanner;

public class Sasta_Calculator{

  public static void main(String [] args){

      Scanner sc = new Scanner(System.in);

      double num1, num2;

      //Taking input

      System.out.println("This is a menu-driven program for performing simple calculations of two numbers only :P");

      System.out.println("Press 1 for Addition");

      System.out.println("Press 2 for Subtraction");

      System.out.println("Press 3 for Multiplication");

      System.out.println("Press 4 for Division");

      System.out.println("Press 5 for Modulus (for finding the remainder)");

      double choice = sc.nextDouble();

      // For Sum

      if (choice == 1){

          System.out.println("You chose Addition!!");

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

          num1 = sc.nextDouble();

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

          num2 = sc.nextDouble();

          double sum = num1 + num2;

          System.out.println("Sum: " + sum);

      }

      // For Difference

      else if (choice == 2){

          System.out.println("You chose Subtraction!!");

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

          num1 = sc.nextDouble();

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

          num2 = sc.nextDouble();

          double dif = num1 - num2;

          System.out.println("Difference: " + dif);

      }

      // For Product

      else if (choice == 3){

          System.out.println("You chose Multiplication!!");

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

          num1 = sc.nextDouble();

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

          num2 = sc.nextDouble();

          double pro = num1 * num2;

          System.out.println("Product: " + pro);

      }

      // For Quotient

      else if (choice == 4){

          System.out.println("You chose Division!!");

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

          num1 = sc.nextDouble();

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

          num2 = sc.nextDouble();

          double quo = num1 / num2;

          System.out.println("Quotient: " + quo);

      }

      // For Remainder

      else if (choice == 5){

          System.out.println("You chose Modulus!!");

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

          num1 = sc.nextDouble();

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

          num2 = sc.nextDouble();

          double rem = num1 % num2;

          System.out.println("Remainder: " + rem);

      }

      // The 'else' statement was not working here, I am always confused with this

      else if (choice != 1 && choice != 2 && choice != 3 && choice != 4 && choice != 5){

      System.out.println("Invalid Input *_*");

      System.out.println("Enter only 1, 2, 3, 4 or 5");

      }

  }

}

___________________________________

These are the possible outputs for all the added conditions.

The underlined values are the inputs.

Outputs:

(i)

This is a menu-driven program for performing simple calculations of two numbers only :P

Press 1 for Addition

Press 2 for Subtraction

Press 3 for Multiplication

Press 4 for Division

Press 5 for Modulus (for finding the remainder)

1

You chose Addition!!

Enter the first number: 25

Enter the second number: 35

Sum: 60.0

(ii)

This is a menu-driven program for performing simple calculations of two numbers only :P

Press 1 for Addition

Press 2 for Subtraction

Press 3 for Multiplication

Press 4 for Division

Press 5 for Modulus (for finding the remainder)

2

You chose Subtraction!!

Enter the first number: 250

Enter the second number: 175

Difference: 75.0

(iii)

This is a menu-driven program for performing simple calculations of two numbers only :P

Press 1 for Addition

Press 2 for Subtraction

Press 3 for Multiplication

Press 4 for Division

Press 5 for Modulus (for finding the remainder)

3

You chose Multiplication!!

Enter the first number: 23

Enter the second number: 17

Product: 391.0

(iv)

This is a menu-driven program for performing simple calculations of two numbers only :P

Press 1 for Addition

Press 2 for Subtraction

Press 3 for Multiplication

Press 4 for Division

Press 5 for Modulus (for finding the remainder)

4

You chose Division!!

Enter the first number: 364

Enter the second number: 7

Quotient: 52.0

(v)

This is a menu-driven program for performing simple calculations of two numbers only :P

Press 1 for Addition

Press 2 for Subtraction

Press 3 for Multiplication

Press 4 for Division

Press 5 for Modulus (for finding the remainder)

5

You chose Modulus!!

Enter the first number: 27

Enter the second number: 5

Remainder: 2.0

(vi)

This is a menu-driven program for performing simple calculations of two numbers only :P

Press 1 for Addition

Press 2 for Subtraction

Press 3 for Multiplication

Press 4 for Division

Press 5 for Modulus (for finding the remainder)

12

Invalid Input *_*

Enter only 1, 2, 3, 4 or 5

___________________________________

Thanks for reading till here ˶ᵔ ᵕ ᵔ˶

Attachments:
Similar questions