Difference between revisions of "Pythagoras"
(Added Question and Code) |
|||
Line 1: | Line 1: | ||
{{1010PrAD|Pythagoras=The name of the program | {{1010PrAD|Pythagoras=The name of the program | ||
− | |Problem= Create a program that will allow the user to enter in the lengths of two sides of a right-angled triangle, and will output the length of the triangle's hypotenuse. The two sides should be entered using two consecutive JOptionPane.showInputDialog boxes, and the result should be given using System.out.println. The input should be taken as two positive decimal values. | + | |Problem= Create a program that will allow the user to enter in the lengths of two sides of a right-angled triangle, and will output the length of the triangle's hypotenuse. The two sides should be entered using two consecutive JOptionPane.showInputDialog boxes, and the result should be given using System.out.println. The input should be taken as two positive decimal values. Assume valid inputs |
The formula for calculating the hypotenuse is <math>a^2 + b^2 = c^2</math>, where a and b are the two sides, and c is the hypotenuse. | The formula for calculating the hypotenuse is <math>a^2 + b^2 = c^2</math>, where a and b are the two sides, and c is the hypotenuse. | ||
Line 38: | Line 38: | ||
String input; | String input; | ||
double sideA; | double sideA; | ||
− | double | + | double sideB; |
// Get input 1 | // Get input 1 | ||
Line 52: | Line 52: | ||
sideB = Double.parseDouble(input); | sideB = Double.parseDouble(input); | ||
− | + | //calculate A^2 | |
− | + | double sideA2 = Math.pow(sideA, 2); | |
− | + | ||
− | + | ||
− | + | ||
− | + | //calculate B^2 | |
− | + | double sideB2 = Math.pow(sideB, 2); | |
− | + | //calculate C^2 | |
− | + | double sideC2 = sideA2 + sideB2; | |
− | + | //calculate C | |
− | + | double sideC = Math.sqrt(sideC2); | |
− | + | //output solution | |
− | + | System.out.println("The length of the hypotenuse is: " + sideC); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
} | } | ||
} | } |
Revision as of 12:08, 6 April 2010
Back to the Program-A-Day homepage
ProblemCreate a program that will allow the user to enter in the lengths of two sides of a right-angled triangle, and will output the length of the triangle's hypotenuse. The two sides should be entered using two consecutive JOptionPane.showInputDialog boxes, and the result should be given using System.out.println. The input should be taken as two positive decimal values. Assume valid inputs The formula for calculating the hypotenuse is <math>a^2 + b^2 = c^2</math>, where a and b are the two sides, and c is the hypotenuse. To solve this problem, you will need to understand:
|
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionThe solution... For the entire code solution, see below. | ||
Code |