java
write a java program to make a calculator with +,-,÷and×sign
Answers
Answer:
java calculator
import java.util.Scanner;
import javax.swing.JOptionPane;
public class javaCalculator { public static void main(String[] args)
{ int num1; int num2; String operation; Scanner input = new Scanner(System.in); System.out.println("please enter the first number"); num1 = input.nextInt();
System.out.println("please enter the second number");
num2 = input.nextInt();
Scanner op = new Scanner(System.in); System.out.println("Please enter operation"); operation = op.next();
if (operation == "+");
{ System.out.println("your answer is" + (num1 + num2)); }
if (operation == "-"); { System.out.println("your answer is" + (num1 - num2)); }
if (operation == "/");
{ System.out.println("your answer is" + (num1 / num2)); }
if (operation == "*")
{ System.out.println("your answer is" + (num1 * num2)); }
}
}
Hope it helps ^_^
This program is in Java
I have also added a variable descriptive table in the attached file
import java.util.Scanner;
class Sasta_Calculator{
static void main(){
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