write a program to make a java calculater
Answers
Following is a simple Java Program which is a menu-driven program based on simple calculation like addition, subtraction, multiplication and division according to user's choice:
/* Java Program Example - Make Calculator */
import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
float a, b, res;
char choice, ch;
Scanner scan = new Scanner(System.in);
do
{
System.out.print("1. Addition\n");
System.out.print("2. Subtraction\n");
System.out.print("3. Multiplication\n");
System.out.print("4. Division\n");
System.out.print("5. Exit\n\n");
System.out.print("Enter Your Choice : ");
choice = scan.next().charAt(0);
switch(choice)
{
case '1' : System.out.print("Enter Two Number : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a + b;
System.out.print("Result = " + res);
break;
case '2' : System.out.print("Enter Two Number : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a - b;
System.out.print("Result = " + res);
break;
case '3' : System.out.print("Enter Two Number : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a * b;
System.out.print("Result = " + res);
break;
case '4' : System.out.print("Enter Two Number : ");
a = scan.nextFloat();
b = scan.nextFloat();
res = a / b;
System.out.print("Result = " + res);
break;
case '5' : System.exit(0);
break;
default : System.out.print("Wrong Choice!!!");
break;
}
System.out.print("\n---------------------------------------\n");
}while(choice != 5);
}
}
Hope it helps ^_^
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 numbers 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