Program to compute the distance between two points

Distance of two points
The distance of two points


Program Description

As shown in the above figure, two points p1(x1,x2) and p2(x2,y2) is given as shown in the above figure, The objective of the program to compute the distance of two points p1 and p2.

C Program

#include<stdio.h>
#include<math.h>
int main() {
float x1,y1,x2,y2,distance;
printf("Enter first point x1,y1: ");
scanf("%f%f",&x1,&y1);
printf("Enter second point x2,y2: ");
scanf("%f%f",&x2,&y2);
distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
printf("Distance = %.2f",distance);
return 0;
}

C++ Program

#include<iostream>
#include<math.h>
using namespace std;

int main() {
float x1,y1,x2,y2,distance;
cout << "Enter first point x1,y1: ";
cin >> x1 >> y1;
cout << "Enter second point x2,y2: ";
cin >> x2 >> y2;
distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
cout << "Distance = : " <<distance;
return 0;
}

Java Program

import java.util.Scanner;
class Distance {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first point (x1,y1): ");
float x1 = sc.nextFloat();
float y1 = sc.nextFloat();
System.out.print("Enter second point (x2,y2): ");
float x2 = sc.nextFloat();
float y2 = sc.nextFloat();
double distance = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
System.out.println("Distance = "+distance);
}
}

Go Lang Program

package main

import (
"fmt"
"math"
)

func main() {
var x1,y1,x2,y2 float64
fmt.Print("Enter first point (x1,y1): ")
fmt.Scan(&x1,&y1)
fmt.Print("Enter second point (x2,y2): ")
fmt.Scan(&x2,&y2)
distance := math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
fmt.Println("Distance = ",distance)
}

PHP Program

<?php
if(isset($_POST['sub'])) {
$x1 = $_POST['x1'];
$y1 = $_POST['y1'];
$x2 = $_POST['x2'];
$y2 = $_POST['y2'];

$distance = sqrt(($x2 - $x1) * ($x2 - $x1) + ($y2 - $y1) * ($y2 - $y1));

echo "Distance = ".$distance;
}
?>

Comments

Popular posts from this blog

Program to calculate telephone bill

Sum of two numbers in go lang

Program to check a year is leap year or not.

String in golang

Identifiers

Arrays in C Language

Reserved Words in Java

Data types in Java

Pointers in C Language

Literals in Java