C Programming

Solved Example

Practice Questions

Exercise Question

Check the points are co-linear or not

Objective:

Input coordinates for 3 points (x1,y1), (x2,y2) and (x3,y3). Write a program to check that all points are fall in one straight line or not.

Program / Solution:

#include<stdio.h>
#include<math.h>

int main() {
	int x1,y1,x2,y2,x3,y3;
	int s1,s2,s3;
	
	printf("Enter first point (x1,y1): ");
	scanf("%d%d",&x1,&y1);
	
	printf("Enter second point (x2,y2): ");
	scanf("%d%d",&x2,&y2);
	
	printf("Enter third point (x3,y3): ");
	scanf("%d%d",&x3,&y3);
	
	s1 = abs(x2 - x1) / abs(y2 - y1); //slop between point-1 and point-2
	s2 = abs(x3 - x2) / abs(y3 - y2); //slop between point-2 and point-3
	s3 = abs(x3 - x1) / abs(y3 - y1); //slop between point-3 and point-1
	
	if((s1 == s2) && (s2 == s3)) {
		printf("Points are co-linear");
	}else {
		printf("Points are not co-linear");
	}
	return 0;
}

Sample Output:

Enter first point (x1,y1): 1 1
Enter second point (x2,y2): 2 2
Enter third point (x3,y3): 3 3
Points are co-linear

More Progarms of Decision Making in C

Calculate profit or loss

Check a number is even or odd

Check a number is positive or negative

Check a year is leap year or not

Find the first day of year

Find the youngest person

Absolute value of a number

Find area of rectangle is greater than perimeter

Check a point is inside,outside or on the circle

Check the axis of a point

Identify a character is alphabet, digit and more

Check the insurance eligibility and calculate premium

Calculate the library penalty charges

Calculate grade of a student