Write a java program to find Distance

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

Find Distance in Java.

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;

  }
}

You May Also Like These Posts:

Leave a Reply

Your email address will not be published.

nineteen − seven =