Home / Programs / Write a C program that would find the length of a straight line formed by two end points, whose co-ordinates would be given as inputs.
Programming Example

Write a C program that would find the length of a straight line formed by two end points, whose co-ordinates would be given as inputs.

👁 3,371 Views
💻 Practical Program
📘 Step by Step Learning
Write a C program that would find the length of a straight line formed by two end points, whose co-ordinates would be given as inputs.

Program Code

/*
 Program: Write a C program that would find the length of a straight line
  formed by two end points, whose co-ordinates would be given as inputs. 
  
 Author: www.atnyla.com  
 
*/ 

#include "stdio.h" 
#include "math.h"

int main( )
{
     float x1, y1, x2, y2, lin_len;  /* (x1,y1) and (x2,y2) are co-ordinates of two points */ 
     printf("\n\n Enter   x-co-ordinate of first point: ");
     scanf("%f", &x1);
 
     printf("\n Enter   y-co-ordinate of first point: ");
     scanf("%f", &y1);

     printf("\n\n Enter x-co-ordinate of second point: ");
     scanf("%f", &x2);

     printf("\n Enter y-co-ordinate of second point: ");
     scanf("%f", &y2);

      lin_len = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));   
      printf("\n The length of the straight line joining the two points is %f", lin_len); 
      return 0;
} 

Output



 Enter   x-co-ordinate of first point: 2

 Enter   y-co-ordinate of first point: 2


 Enter x-co-ordinate of second point: 6

 Enter y-co-ordinate of second point: 8

 The length of the straight line joining the two points is 7.211102 

Explanation

None

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.