C Program to Find Distance Between Two Points

C Program to Find Distance Between Two Points

C program to find the distance between two points; Through this tutorial, we will learn how to find distance between two points in c program.

Algorithm to Find Distance Between Two Points

Use the following algorithm to write a program to find the distance between two points; as follows:

  • Step 1: Start program.
  • Step 2: Read two points numbers from user and store them into variables.
  • Step 3: Find distance between two points using this formula (sqrt( (x2 – x1)*(x2 – x1) + (y2 – y1)*(y2 – y1) )) and store result in variable.
  • Step 4: Print distance between two points.
  • Step 5: End program.

C Program to Find Distance Between Two Points

#include<stdio.h>  
#include<math.h>  
  
int main()  
{  
    float x1, y1, x2, y2, distance;  
  
    printf("Enter point 1 (x1, y1)\n");  
    scanf("%f%f", &x1, &y1);  
  
    printf("Enter point 2 (x2, y2)\n");  
    scanf("%f%f", &x2, &y2);  
  
    distance = sqrt( (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) );  
  
    printf("Distance between (%0.2f, %0.2f) and (%0.2f, %0.2f) is %0.2f\n", x1, y1, x2, y2, distance);  
  
    return 0;  
}  

The output of the above c program; as follows:

Enter point 1 (x1, y1) :- 10 20
Enter point 2 (x2, y2) :- 25 30
Distance between (10.00, 20.00) and (25.00, 30.00) is 18.03

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *