Implement a hand held calculator. calculator that models are It should have following functions:
1.addition
2. Subtraction
3.Multiplication
4.Division
5.Remainder
Now use switch -case statement for two numbers to design a to a calculator any
Answers
Answer:
import java.util.Scanner;
public class calc
{
public static void main(String[] args)
{
int choice;
Scanner sc=new Scanner(System.in);
System.out.print("Enter 1 for addition .\nEnter 2 for subtraction.\n Enter 3 for multiplication.\nEnter 4 for division. ")
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("ADDITION");
System.out.print("ENTER A NUMBER: ")
double a=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double b=sc.nextDouble();
double sum=a+b;
System.out.println("Sum is :"+sum);
case 2:
System.out.println("SUBTRACTION");
System.out.print("ENTER A NUMBER: ")
double x=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double y=sc.nextDouble();
double diff=x-y;
System.out.println("Difference is :"+diff);
case 3:
System.out.println("MULTIPLICATION");
System.out.print("ENTER A NUMBER: ")
double p=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double q=sc.nextDouble();
double mult=p*q;
System.out.println("Product is :"+mult);
case 4:
System.out.println("DIVISION");
System.out.print("ENTER A NUMBER: ")
double c=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double d=sc.nextDouble();
double quotient=c/d;
System.out.println("Quotient is :"+quotient);
case 5:
System.out.println("REMAINDER");
System.out.print("ENTER A NUMBER: ")
double e=sc.nextDouble();
System.out.print("ENTER A NUMBER: ")
double f=sc.nextDouble();
double rem=e%f;
System.out.println("Remainder is :"+rem);
}
}
}
Explanation: