Go to Top

Monday 3 October 2011

To find the distance between two points

Question 53 : Write a program in Java to find and print the distance between two point whose X and Y - axis coordinates have been taken as input from the user.

Java Program :

import java.io.*;
class point_distance
{
  static int x,x1,y,y1;
  static double d;
  static void input()throws IOException
  {
      BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
      System.out.print("For point A : ");
      System.out.print("\n\nEnter abcisssa (x-co-ordinate) : ");
      x=Integer.parseInt(b.readLine());
      System.out.print("\nEnter ordinate (y-co-ordinate) : ");
      y=Integer.parseInt(b.readLine());
      System.out.print("\nFor point B : ");
      System.out.print("\n\nEnter abcisssa (x-co-ordinate) : ");
      x1=Integer.parseInt(b.readLine());
      System.out.print("\nEnter ordinate (y-co-ordinate) : ");
      y1=Integer.parseInt(b.readLine());
      cal();
      display();
    }
    static void cal()
    {
        d=Math.sqrt(((x-x1)*(x-x1))+((y-y1)*(y-y1)));
    }
    static void display()
    {
        System.out.print("\nDistance between A ("+x+","+y+") and B ("+x1+","+y1+") : "+d+" units");
    }
}
      

No comments:

Post a Comment

ShareThis