Calculate the Sum, Product, Difference & Quotient in Java
In this Java program, we will calculate the user inputs by Addition, multiplication, subtraction & division. The output will be displayed in Graphical User interface (GUI).
This is the solution of Deitel & Deitel exercises 2.16:
- Write an application that asks the user to enter two numbers from the users and prints the sum, product, difference and quotient (division) of the numbers.
import javax.swing.*;
/**
* Exersice No.04 Display the number is larger or equal
* Created by Airgildgreat on 3/27/2017.
*/
public class DisplayLargerEqualNumbers {
public static void main(String args[]){
//Declaring the variables
String fNumber, sNumber;
int num1, num2, sum, product, diff, quo;
//Read the inputs of the user
fNumber = JOptionPane.showInputDialog("Enter the first number: ");
sNumber = JOptionPane.showInputDialog("Enter the second number: ");
//Convert the input string into integers
num1 = Integer.parseInt(fNumber);
num2 = Integer.parseInt(sNumber);
//Initialize the inputs
sum = num1 + num2;
product = num1 * num2;
diff = num1 - num2;
quo = num1 / num2;
//Prints the result in Graphical User Interface
JOptionPane.showMessageDialog(null, "The sum is: " + sum + "\nThe product is: " + product
+ "\nThe Difference is: " + diff + "\nThe Quotient is: " + quo,"Largest and Smallest Program in GUI", JOptionPane.INFORMATION_MESSAGE);
}
}
Here is the output. In this case assumes user input the first number "2" & second number "3"
+ "\nThe Difference is: " + diff + "\nThe Quotient is: " + quo,"Largest and Smallest Program in GUI", JOptionPane.INFORMATION_MESSAGE);
}
}
Here is the output. In this case assumes user input the first number "2" & second number "3"
Comments
Post a Comment