X & Y Intercepts Program in C

Let’s have a look at the X & Y intercepts program in C Programming Language. The intercepts of a graph are points at which the graph crosses the axes.

The graphic definition of both x- and y-intercepts is very simple.

The x-intercepts are where the line crosses the x-axis. Similarly, y-intercepts are where the line crosses the y-axis.

X & Y Intercepts Program in C

#include<stdio.h>
int main()
{
int m,c,x,y;
printf(“Enter the value of m\n”);//
scanf(“%d”,&m);
printf(“Enter the value of c\n”);
scanf(“%d”,&c);
printf(“The line equation is y=%dx+%d\n”,m,c);
y=m*0+c;
x=-c/m;
printf(“The x intercept is %d\n”,x);
printf(“The y intercept is %d\n”,y);
return 0;
}

Finding the equation of a Line Given Two End Points

#include<stdio.h> 
#include<math.h>
void main()
{
    float slope, intercept;
    float x1, y1, x2, y2;
    float dx, dy;
    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;
    // y = mx + c
    // intercept c = y - mx
    intercept = y1 - slope * x1; // which is same as y2 - slope * x2
    printf("Equation of the line with end points (%.2f, %.2f) and (%.2f, %.2f) : Y = %.2fX %c %.2f\n", x1, y1, x2, y2, slope, (intercept < 0) ? ' ' : '+',  intercept);
}

Output

Enter X1: 2
Enter Y1: 3
Enter X2: 5
Enter Y2: 7
Equation of the line with end points (2, 3 and (5, 7) : Y = 1.33333X +0.333333

Leave a Comment

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