The java program given below will calculate the distance of two points using the mathematical rule c2=a2+b2. Basically we have to found the value of c from the equation. So we need to use the Square Root mathematical operation using Math.sqrt.
That's why we have to import a class using the line import java.lang.*;. Otherwise the method Math.sqrt will not function and obviously will return error message while compiling the program.
As we have to find out c from the equation c2=a2+b2, so we need to know the value of a and b. The program given below won't get any value from the keyboard. The values are predefined for all the variables a, b and c. c is initialized with the value 0, so that there shouldn't stored any garbage value.
That's why we have to import a class using the line import java.lang.*;. Otherwise the method Math.sqrt will not function and obviously will return error message while compiling the program.
As we have to find out c from the equation c2=a2+b2, so we need to know the value of a and b. The program given below won't get any value from the keyboard. The values are predefined for all the variables a, b and c. c is initialized with the value 0, so that there shouldn't stored any garbage value.
import java.lang.*; public class Dis2Point{ public static void main(String [] args){ double a=5, b=2, c=0; c=Math.sqrt(a*a+b*b); System.out.println("Distance is="+c); } }
No comments:
Post a Comment