Pythagorean theorem calc: find a, b=n/a, c=n/a

Last Updated On Thursday 2nd Jun 2022

The Pythagorean Theorem states that the sum of the squared sides of a right triangle equals the length of the hypotenuse squared.

pythagorean theorem calc: find a, b=n/a, c=n/a

Pythagorean Theorem Calculator

	a^2 + b^2 = c^2
	

Solving for a

	a^2 = c^2 - b^2
	

b – leg
c – hypotenuse

make sure c > a

	c = 6;
b = 5;
	

3.31662

Pythagorean Theorem in Java

	import  java.util.Scanner;

public class Main {
    // c^2 = a^2 + b^2

    public static void main(String[] args) {
        //variable
        double a, b, c;
        char x;
        Scanner in = new Scanner(System.in);

        while (true){
            System.out.print("Which side are you looking for [a, b, c]: ");
            x = in.next().charAt(0);
            if (Character.toLowerCase(x) == 'a'){

                System.out.print("give me side (c): ");
                c = in.nextDouble();

                System.out.print("give me side (b): ");
                b = in.nextDouble();

                calculate_sides(c, b, x);
            }else if (Character.toLowerCase(x) == 'b'){
                System.out.print("give me side (c): ");
                c = in.nextDouble();

                System.out.print("give me side (a): ");
                a = in.nextDouble();

                calculate_sides(c, a, x);
            } else if(Character.toLowerCase(x) == 'c'){
                System.out.print("give me side (a): ");
                a = in.nextDouble();

                System.out.print("give me side (b): ");
                b = in.nextDouble();

                calculate_sides(a, b, x);
            }

            System.out.println();

            System.out.print("Do you want to exit?[if you want to exit enter (y)] ");
            x = in.next().charAt(0);

            if (Character.toLowerCase(x) == 'y'){
                break;
            }else{
                continue;
            }

        }

    }

    public static void calculate_sides(double x, double y, char z){
        double result;
        if (Character.toLowerCase(z) == 'a'){
            result =  Math.sqrt(Math.pow(x, 2) - Math.pow(y, 2));
            System.out.printf("%f", result);
        } else if (Character.toLowerCase(z) == 'b'){
            result =  Math.sqrt(Math.pow(x, 2) - Math.pow(y, 2));
            System.out.printf("%f", result);
        } else {
            result =  Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
            System.out.printf("%f", result);
        }
    }
}
	
	Which side are you looking for [a, b, c]: a
give me side (c): 4
give me side (b): 3
2.645751