Java Program to find the distance between two points in 2D space

Description

Get x and y coordinates of two points(x1,y1,x2,y2) and find distance between two points
Input 1:
3 4
4 5
Output1:
1.414

Solution

import java.util.*;
import java.lang.Math.*;
class Distance
{
 
  public static void main(String args[])
  {
   int x1,x2,y1,y2;
   int x,y;
   double distance;
   Scanner sc=new Scanner(System.in);
   System.out.println("Enter The Coordinates:");
   
   x1=sc.nextInt();
   x2=sc.nextInt();
   y1=sc.nextInt();
   y2=sc.nextInt();

   x=x2-x1;
   y=y2-y1;
   distance=Math.sqrt((x*x)+(y*y));
   System.out.println("The Distance Is:"+distance );
  }

}

Explanation

Get x1,y1,x2 and y2 coordinates of two points(using scanner class)

Calculate distance by the eculdian distance formula. Math.sqrt is function available in java library to find square root.

Print distance (using system.out.println).
Ramesh P Natarajan

Author - Ramesh P Natarajan

Learner, Professor, Technology Trainer, Software Developer and Content Marketer, very much passionate in the field of Software Application Development, Machine Learning and Search Engine Optimization.

Write to: admin@decodeschool.com





Interesting Pattern Programs: for more Patterns

Input:
5
Output:
*
**
***
****
*****
****
***
**
*Solution
Input:
5
Output:
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *Solution
Input:
5
Output:
*        *
**      **
***    ***
****  ****
**********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********Solution
Input:
5
Output:
*       *
**     **
***   ***
**** ****
*********
**** ****
***   ***
**     **
*       *Solution
Input:
5
Output:
*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********Solution