Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
so I know how to find the hypotenuse but I am unable to use and find the correct formula to find the length of the other sides in the right triangle.
the formula is to be kept in the double formula2 variable

import java.util.Scanner;
public class pythagorean{
    public static void main(String[]args){
        Scanner keyboard = new Scanner(System.in);
        int option = keyboard.nextInt();
        int side1 = keyboard.nextInt();
        int side2 = keyboard.nextInt();
        double formula = Math.sqrt((side1*side1) + (side2*side2));
        double formula2 = 
        if (option == 1){
            System.out.format("%.1f",formula);
        }
        else if (option == 2){
            System.out.format("%.1f",formula2);
        } 
    }
}


What I have tried:

The code i submitted above is what i have tried so far, but i cant get the right formula
Posted
Updated 19-Feb-20 21:09pm
v2

1 solution

This is a mathematic problem rather than a coding one.
If a and b are the sides of the right triangle and c its hypothenuse, the following relations stand:
c² = a² + b² => c = sqrt(a² + b²)
a² = c² - b² => a = sqrt(c² - b²)
b² = c² - a² => b = sqrt(c² - a²)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900