Software & Finance





Turbo C - Calculating Slope of a Line Given Two End Points





Here is the Turbo C program for Calculating Slope of a Line Given Two End Points

 

It uses the following formula given points are (x1,y1) and (x2, y2)

 

slope = (y2 - y1) / (x2 - x1)

 


Source Code


#include <stdio.h>

#include <math.h>

 

void main()

{

    float slope;

    float x1, y1, x2, y2;

    float dx, dy;

 

    printf("Program to find the slope of a line given two end points\n");

 

    printf("Enter X1: ");

    scanf("%f", &x1);

 

    printf("Enter Y1: ");

    scanf("%f", &y1);

 

    printf("Enter X2: ");

    scanf("%f", &x2);

 

    printf("Enter Y2: ");

    scanf("%f", &y2);

 

    dx = x2 - x1;

    dy = y2 - y1;

 

    slope = dy / dx;

    printf("Slope of the line with end points (%.4f, %.4f) and (%.4f, %.4f) = %.4f", x1, y1, x2, y2, slope);

}

Output


 

Program to find the slope of a line given two end points

Enter X1: 2.5

Enter Y1: 7.5

Enter X2: 12.5

Enter Y2: 18

Slope of the line with end points (2.5, 7.5 and (12.5, 18) = 1.05

Press any key to continue . . .