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:
Morae Q!
- Convert from binary number to decimal number.
- Swap two numbers by using division and multiplication.
- Compute foot and inches into centimetres.
- Concatenate Strings.
- Convert lowercase letters to uppercase letters.
- Compute the sum of all the digits of N.
- Compute the area of the pentagon.
- Get input using scanner.
- Find the distance between two points (x1,y1) and (x2,y2).
- Finding patterns in a file using frep and egrep .
- Detecting a cycle in a directed graph using Depth First Traversal.
- Topological ordering in a Graph.
- Storing graphs using adjacency list.
- Introduction to graph theory.
- Graph Adjacency Matrix pseudo code.
- Adjacency Matrix for storing graphs.
- Dijkstra’s shortest path algorithm .
- Breadth first search (BFS) Algorithm.
- Find the multiple of given number in the list of integers.
- Finding patterns in files using grep .