Skip to content
October 7, 2013 / TDS Software

Java Q&A

Write a Java program to perform a mathematical calculation between two numbers entered
by the user. Whenever you run the program main method should display an output (main
menu) as given below.
Get the mathematical operation to be performed from the user as a keyboard input.
Once the mathematical operation is selected get the two number as keyboard inputs.
Display the result of operation and display the main menu again.
You must perform calculation between numbers until user decided to exit from the
Save your answer as “”.


* : For multiplication
/ : For division
+ : For addition
– : For Subtraction
Any other character : To exit
Mathematical Operation : _



class Question11{
public static void main(String[] args) {
System.out.println(“* : For multiplication”);
System.out.println(“/ : For division”);
System.out.println(“+ : For addition”);
System.out.println(“- : For Subtraction”);
System.out.println(“Any other character : To exit”);

BufferedReader bf = new BufferedReader (new InputStreamReader (;
System.out.print(“Mathematical Operation : “);
String inp =””;
inp = bf.readLine();

Double n1, n2;
System.out.print(“Enter a number : “);
n1 =Double.parseDouble(bf.readLine());

System.out.print(“Enter another number : “);
n2 =Double.parseDouble(bf.readLine());

char oprt = inp.charAt(0);
System.out.print( “Answer : ” );
System.out.print( n1 * n2 );
}else if(oprt == ‘/’){
System.out.print( n1 / n2 );
}else if(oprt == ‘+’){
System.out.print( n1 + n2 );
}else if(oprt == ‘-‘){
System.out.print( n1 – n2 );
}catch (IOException e){



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: