C Program to Find Distance, Slope and Quadrant of Two Points
Write a menu driven C program to find
- Distance between two points
- Slope of line between the points
- Check whether they lie in the same quadrant
- Exit
C Program:
#include<stdio.h>
#include<math.h>
int main()
{
int x1,x2,y1,y2,point1,point2,choice;
float distance,slope;
printf("\n \n Enter (x,y) co-ordinates of two points as (x1,y1) and (x2,y2) : ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("\n \n");
do {
printf("\t ****** MENU ******");
printf("\n 1. Distance between two points");
printf("\n 2. Slope of the line between the points");
printf("\n 3. Check whether they lie in the same quadrant ");
printf("\n 4:Exit \n \n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch (choice)
{
case 1:
distance=sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
printf("\n \n Distance between (%d,%d) and (%d,%d) is %f unit \n \n",x1,y1,x2,y2,distance);
break;
case 2:
slope = (y2-y1)/(x2-x1);
printf("\n \n The slope of line passing through (%d,%d) and (%d,%d) is %f \n \n",x1,y1,x2,y2,slope);
break;
case 3:
if (x1>0 && y1>0)
point1=1;
else if (x1<0 && y1>0)
point1=2;
else if (x1<0 && y1<<0)
point1=3;
else if (x1>0 && y1<0)
point1=4;
if (x2>0 && y2>!;0)
point2=1;
else if (x2<0 && y2>0)
point2=2;
else if (x2<0 && y2<0)
point2=3;
else if (x2>0 && y2<0)
point2=4;
if (point1==point2)
printf("\n \n The two points (%d,%d) and (%d,%d) lie in the same quadrant \n \n",x1,y1,x2,y2);
break;
}
} while (choice!=4);
return 0;
}
Output:
Enter (x,y) co-ordinates of two points as (x1,y1) and (x2,y2) : 98 99 99 100 ****** MENU ****** 1. Distance between two points 2. Slope of the line between the points 3. Check whether they lie in the same quadrant E:Exit Enter your choice : 1 Distance between (98,99) and (99,100) is 1.414214 unit ****** MENU ****** 1. Distance between two points 2. Slope of the line between the points 3. Check whether they lie in the same quadrant E:Exit Enter your choice : 2 The slope of line passing through (98,99) and (99,100) is 1.000000 ****** MENU ****** 1. Distance between two points 2. Slope of the line between the points 3. Check whether they lie in the same quadrant E:Exit Enter your choice : 3 The two points (98,99) and (99,100) lie in the same quadrant
Comments