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
program.
Save your answer as “Question11.java”.

 

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

 

import java.io.*;

class Question11{
public static void main(String[] args) {
while(true){
System.out.println(“Enter”);
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”);

try{
BufferedReader bf = new BufferedReader (new InputStreamReader (System.in));
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 : ” );
if(oprt==’*’){
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 );
}else{
System.exit(1);
}
System.out.println(“\n__________________________”);
}catch (IOException e){
e.printStackTrace();
}

}
}
}

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: