This program will tell you the length of the hypotenuse side using the Pythagoras theorem.

For more details on Pythagoras theorem, please click here.

The program asks the user to input the length of two sides and then it calculates the hypotenuse using the Pythagoras theorem.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.*; //Importing util for Scanner public class Pythagoras { public static void main(String[] args) { Scanner MyScan = new Scanner(System.in); //Initializing our scanner System.out.println("This program will calculate the length of the hypotenuse for you."); System.out.println("Enter the first side:"); //Asks user to input the first side double side1 = MyScan.nextDouble(); //Scans the input and stores in 'side1' System.out.println("Enter the Second side:"); //Asks user to input the second side double side2 = MyScan.nextDouble(); //Scans the input and stores in 'side2' double hypotenuse = Math.pow(side1, 2) + Math.pow(side2, 2); //First step is to make hypotenuse=side1^2+side2^2 hypotenuse = Math.sqrt(hypotenuse); //Find the square root of the above calculated hypotenuse System.out.println("The hypotenuse is: " + hypotenuse); //Prints the correct hypotenuse } } |

Output:

Tested in Eclipse.

The following two tabs change content below.

#### Vlad

#### Latest posts by Vlad (see all)

- Code jam “Tic-Tac-Toe-Tomek” solution in java - April 19, 2013
- Code jam “Minimum Scalar product” solution in java - March 18, 2013
- Code jam Store Credit solution in java - March 10, 2013