Write a Program that accepts four int inputs(x1,y1,x2,y2) as the coordinates of two points. Calculate the distance between the two points using the below formula.
Formula : square root of((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)). Then, Round the result to return an int.
Input and Output Format
- Input consists of four integers.
- The output consists of a single integer.
Refer sample output for formatting specifications
Sample Input 1:
3
4
5
2
Sample Output 1:
3
Sample Input 2:
3
1
5
2
Sample Output 2:
2
Program to Find Distance in Java
Following are the steps to find the distance:
- Input 4 integer values from the user.
- Pass these values to display() method.
- Inside the method, calculate the distance using the below mention formula:
Math.round(Math.sqrt(((a - c) * (a - c)) + ((b - d) * (b - d))))
- At last, return the calculated value to the user.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); int n = display(a, b, c, d); System.out.print(n); } public static int display(int a, int b, int c, int d) { long q = (int) Math.round(Math.sqrt(((a - c) * (a - c)) + ((b - d) * (b - d)))); return (int) q; } }
Output
Find Distance between two points in 3D
Write a Program that accepts six int inputs(x1,y1,x2,y2,z1,z2) as the coordinates of three points. Calculate the distance between the two points using the below formula.
Formula : square root of((x2-x1)2+(y2-y1)2+(z2-z1)2). Then, Round the result to return an int.
Input and Output Format
- Input consists of six integers.
- The output consists of a single integer.
Refer sample output for formatting specifications
Sample Input 1:
7
4
3
17
6
2
Sample Output 1:
10
Sample Input 2:
6
5
7
8
2
9
Sample Output 2:
4
Following are the steps to find the distance between two points in 3D:
- Input 6 integer values from the user.
- Pass these values to display() method.
- Inside the method, calculate the distance using the below mention formula:
Math.round(Math.sqrt(Math.pow(d-a, 2)+Math.pow(e-b,2)+Math.pow(f-c,2)))
- At last, return the calculated value to the user.
package com.demo2; import java.util.Scanner; public class Distance { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); int e = sc.nextInt(); int f = sc.nextInt(); int n = display(a, b, c, d, e, f); System.out.print(n); } public static int display(int a, int b, int c, int d, int e, int f) { long q = (int) Math.round(Math.sqrt(Math.pow(d-a, 2)+Math.pow(e-b,2)+Math.pow(f-c,2))); return (int) q; } }
Output
Java Program to find the distance between 2 points on Earth
Write a Program that accepts 4 int inputs(x1,y1,x2,y2) as the coordinates of two points. Calculate the distance between the two points using the below formula.
In this example, we will be using the Haversine formula. Here, the value 3963.0 is the radius of the earth in KM.
Formula : 3963.0 * arccos[(sin(x1) * sin(x2)) + cos(x1) * cos(x2) * cos(y2 – y1)].
Input and Output Format
- Input consists of four integers.
- The output consists of a single integer.
Refer sample output for formatting specifications
Sample Input :
21.045521
75.801094
51.507351
-0.127758
Sample Output :
6494.82732058751
Following are the steps to find the distance between two points in 3D:
- Input 4 integer values from the user and convert them into radians.
- Pass these values to display() method.
- Inside the method, first, calculate the difference between longitude and latitude and store them in variables d1 and d2.
- Next, calculate the distance with the below mention formula:
Math.pow(Math.sin(d1 / 2), 2)+ Math.cos(a) * Math.cos(b)* Math.pow(Math.sin(d2 / 2),2) double p = 2 * Math.asin(Math.sqrt(result));
- At last, return the calculated value to the user.
package com.demo2; import java.util.Scanner; public class Distance { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double a = Math.toRadians(sc.nextDouble()); //latitude 1 double b = Math.toRadians(sc.nextDouble()); //latitude 2 double c = Math.toRadians(sc.nextDouble()); //longitude 1 double d = Math.toRadians(sc.nextDouble()); //longitude 2 double n = display(a, b, c, d); System.out.print(n); } public static double display(double a, double b, double c, double d) { double d1 = d - c; double d2 = b - a; double radius = 6371; // Radius of Earth in KM double result = Math.pow(Math.sin(d1 / 2), 2)+ Math.cos(a) * Math.cos(b)* Math.pow(Math.sin(d2 / 2),2); double p = 2 * Math.asin(Math.sqrt(result)); return(p * radius); } }
Output
Thus, in this way, we can find the distance between any two points in Java.