Menu Close

Find the distance between two points (x1,y1) and (x2,y2)

Write a program that prompts the user to enter two points (x1, y1) and (x2, y2) and displays their distance between them.

Use Double Data Type and String.format(” %.2f”, outputValue);

INPUT
2  3  4  5

OUTPUT
2.83
import java.util.*;
import java.io.*;
import java.lang.Math;
public class temp {
	 public static void main(String[] args) {
       double x1,y1,x2,y2,outputValue;
       Scanner scanobj = new Scanner(System.in);
       System.out.println("Enter the first point(x1,y1):  ");x1 = scanobj.nextDouble();
       y1 = scanobj.nextDouble();
       System.out.println("Enter the second point(x2,y2):  ");
       x2 = scanobj.nextDouble();
       y2 = scanobj.nextDouble();
       outputValue = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
       System.out.println("\nDistance between the two point is:  "+String.format("%.2f",outputValue));
	}
}

INPUT_1:
Enter the first point(x1,y1):
2  3
Enter the second point(x2,y2):
4  5

OUTPUT:
Distance between the two point is:  2.83


INPUT_2:
Enter the first point(x1,y1):
2  1
Enter the second point(x2,y2):
3  4

OUTPUT:
Distance between the two point is:  3.16


INPUT_3:
Enter the first point(x1,y1):
56  65
Enter the second point(x2,y2):
23  44

OUTPUT:
Distance between the two point is:  39.12


INPUT_4:
Enter the first point(x1,y1):
100  120
Enter the second point(x2,y2):
200  250

OUTPUT:
Distance between the two point is:  164.01


INPUT_5:
Enter the first point(x1,y1):
89  56
Enter the second point(x2,y2):
89  70

OUTPUT:
Distance between the two point is: 14.00


INPUT_6:
Enter the first point(x1,y1):
60  5
Enter the second point(x2,y2):
81  10

OUTPUT:
Distance between the two point is:  21.59


ILLUSTRATION:

Executed using javac in terminal (linux)Ubuntu

Morae Q!

  1. Convert from binary number to decimal number.
  2. Swap two numbers by using division and multiplication.
  3. Compute foot and inches into centimetres.
  4. Concatenate Strings.
  5. Convert lowercase letters to uppercase letters.
  6. Compute the sum of all the digits of N.
  7. Compute the area of the pentagon.
  8.  Get input using scanner.
  9. Find the distance between two points (x1,y1) and (x2,y2).
  10. Finding patterns in a file using frep and egrep .
  11. Detecting a cycle in a directed graph using Depth First Traversal.
  12. Topological ordering in a Graph.
  13. Storing graphs using adjacency list.
  14. Introduction to graph theory.
  15. Graph Adjacency Matrix pseudo code.
  16. Adjacency Matrix for storing graphs.
  17. Dijkstra’s shortest path algorithm .
  18. Breadth first search (BFS) Algorithm.
  19. Find the multiple of given number in the list of integers.
  20. Finding patterns in files using grep .