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

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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