Computer Science, asked by anmol3247, 8 months ago

write a java program to a make calculator
no spams​

Answers

Answered by xjfreddie
1

Answer:

This is written in c# so just change the output to java and it works

Explanation:

using System;

class Program

   {

       static void Main(string[] args)

       {

           int num1;

           int num2;

           string operand;

           float answer;

           Console.Write("Please enter the first Number: ");

           num1 = Convert.ToInt32(Console.ReadLine());

           Console.Write("Please enter an operand (+, -, /, *): ");

           operand = Console.ReadLine();

 

 

           Console.Write("Please enter the second Number: ");

           num2 = Convert.ToInt32(Console.ReadLine());

 

           switch (operand)

           {

               case "-":

                   answer = num1 - num2;

                   break;

               case "+":

                   answer = num1 + num2;

                   break;

               case "/":

                   answer = num1 / num2;

                   break;

               case "*":

                   answer = num1 * num2;

                   break;

               default:

                   answer = 0;

                   break;

           }

 

           Console.WriteLine(num1.ToString() + " " + operand + " " + num2.ToString() + " = " + answer.ToString());

 

           Console.ReadLine();

       }

   }

Answered by Shivu516
0

Hope it helps ^_^

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