/*
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;
}
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
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.