12 June 2018

Distance between 2 points


Input:
The first line of the input contains the number of test cases T. Each line contains 4 integers denoting those 2 points as (x1,y1), (x2,y2). 

Output: 
For each test case print in a single line the distance between the two points.

 

Input:
4
0 0 2 -2
-20 23 -15 68
30 37 79 -51
-69 63 57 11


Output:
3
45
101
136


 //formula of two co-ordinate points-->sqrt((y1-x1)^2+(y2-x2)^2)

import java.util.*;
public class NewClass5 {
      
    void coordinate(int x1,int x2,int y1,int y2)
    {
        x1=y1-x1; x2=y2-x2;
        y1=x1*x1;  y2=x2*x2;
        System.out.println((int)Math.sqrt(y1+y2));
    }

    public static void main(String args[])
    {
        Scanner s=new Scanner(System.in);
        int x1,x2,y1,y2,n;
        NewClass5 ob=new NewClass5();
           n=s.nextInt();
        for(int i=0;i<n;i++){
            x1=s.nextInt();
            x2=s.nextInt();
            y1=s.nextInt();
            y2=s.nextInt();
           
            ob.coordinate(x1,x2,y1,y2);
        }
             
    }
}

No comments:

Post a Comment