Write a program to simulate a calculator without graphical user interface. The c
ID: 3639609 • Letter: W
Question
Write a program to simulate a calculator without graphical user interface. The calculator can do addition, subtraction, multiplication, division and calculate square root.Here are the specifications of the program:
1) When the program stars, display “Welcome to use this simple calculator developed by XXX(your name)!”
2) Display “Enter 1 for addition, enter 2 for subtraction, enter 3 for multiplication, enter 4 for division, enter 5 for square root” in a new line.
3) Read customer’s input. If the user enters 1 – 4, then ask user to enter two numbers. If the user enters 5, ask user to enter one number.
4) Display the result.
Explanation / Answer
Hope this will Help... import java.util.Scanner; public class calculator { public static void main(String args[]){ System.out.println("Welcome to use this simple calculator developed by XYZ!"); System.out.println("Enter 1 for Addition"); System.out.println("Enter 2 for Subtraction"); System.out.println("Enter 3 for Multiplication"); System.out.println("Enter 4 for Devision"); System.out.println("Enter 5 for Square Root"); System.out.println("Enter your Choice:"); Scanner s=new Scanner(System.in); int choice=s.nextInt(); int first_num,second_num,result; switch(choice){ case 1: System.out.println("Enter First Number:"); first_num=s.nextInt(); System.out.println("Enter Second Number:"); second_num=s.nextInt(); result=first_num+second_num; System.out.println("Sum of two Numbers:"+result); break; case 2: System.out.println("Enter First Number:"); first_num=s.nextInt(); System.out.println("Enter Second Number:"); second_num=s.nextInt(); result=first_num-second_num; System.out.println("Difference of two Numbers:"+result); break; case 3: System.out.println("Enter First Number:"); first_num=s.nextInt(); System.out.println("Enter Second Number:"); second_num=s.nextInt(); result=first_num*second_num; System.out.println("Multiplication of two Numbers:"+result); break; case 4: System.out.println("Enter First Number:"); double f_num=s.nextDouble(); System.out.println("Enter Second Number:"); double s_num=s.nextDouble(); double result1=f_num/s_num; System.out.println("Division of two Numbers:"+result1); break; case 5: System.out.println("Enter a Number:"); first_num=s.nextInt(); double result2=Math.sqrt(first_num); System.out.println("Square Root is:"+result2); break; default: System.out.println("You have entered a wrong choice!!!"); } } } ///Complete here/// if you want that it should run again and again until u want to stop it for example it should stop when user enter 6 then do as the following. also u didn't mention that either to do with if-else or switch, i am doing with switch. U can PM me for further help. Code for running the code until user enters 6. import java.util.Scanner; public class calculator { public static void main(String args[]){ int choice; do{ System.out.println("Welcome to use this simple calculator developed by XYZ!"); System.out.println("Enter 1 for Addition"); System.out.println("Enter 2 for Subtraction"); System.out.println("Enter 3 for Multiplication"); System.out.println("Enter 4 for Devision"); System.out.println("Enter 5 for Square Root"); System.out.println("Enter 6 for Exit"); System.out.println("Enter your Choice:"); Scanner s=new Scanner(System.in); choice=s.nextInt(); int first_num,second_num,result; switch(choice){ case 1: System.out.println("Enter First Number:"); first_num=s.nextInt(); System.out.println("Enter Second Number:"); second_num=s.nextInt(); result=first_num+second_num; System.out.println("Sum of two Numbers:"+result); break; case 2: System.out.println("Enter First Number:"); first_num=s.nextInt(); System.out.println("Enter Second Number:"); second_num=s.nextInt(); result=first_num-second_num; System.out.println("Difference of two Numbers:"+result); break; case 3: System.out.println("Enter First Number:"); first_num=s.nextInt(); System.out.println("Enter Second Number:"); second_num=s.nextInt(); result=first_num*second_num; System.out.println("Multiplication of two Numbers:"+result); break; case 4: System.out.println("Enter First Number:"); double f_num=s.nextDouble(); System.out.println("Enter Second Number:"); double s_num=s.nextDouble(); double result1=f_num/s_num; System.out.println("Division of two Numbers:"+result1); break; case 5: System.out.println("Enter a Number:"); first_num=s.nextInt(); double result2=Math.sqrt(first_num); System.out.println("Square Root is:"+result2); break; case 6: System.out.println("Good Bye!"); break; default: System.out.println("You have entered a wrong choice!!!"); } }while(choice!=6); } } Good Luck.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.