Write a program in java to calculate the hypotenuse of a right triangle using Py
ID: 3638659 • Letter: W
Question
Write a program in java to calculate the hypotenuse of a right triangle using Pythagoras theorem. You must write a method to do this i.e., the computation should be done inside a method. Prompt the user to enter the base and the height of a right triangle. In the output, show the hypotenuse of the triangle. You should pass the base and height as parameters to your method and your method should return the hypotenuse.Example:
This is a program to calculate the hypotenuse of a right triangle.
Please enter the base: (user enters 3)
Please enter the height: (user enters 4)
The hypotenuse of this right angled triangle with base 3 and height 4 is 5.000
The output should be a double value.
Explanation / Answer
please rate - thanks
import java.util.*;
public class main
{
public static void main(String [] args)
{ int base,height;
double hypot;
Scanner in=new Scanner(System.in);
System.out.println("This is a program to calculate the hypotenuse of a right triangle.");
System.out.print("Please enter the base: ");
base=in.nextInt();
System.out.print("Please enter the height: ");
height=in.nextInt();
hypot=hypotenus(base,height);
System.out.println("The hypotenuse of this right angled triangle with base "+base+" and height "+height+" is "+hypot);
}
public static double hypotenus(int base, int height)
{return Math.sqrt((double)base*base+height*height);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.