Code:
#include <stdio.h> /* Lab 1 program to find the Area/Perimeter of Rectangles, Triangles, Circles, depending on user input*/#include <math.h>
void main()
{
//program to calculate area of rectangle/cirlce/triangle using Cartesian coordinates entered by user
int choice;
float x1,y1,x2,y2,x3,y3,x4,y4,side_1,side_2,side_3,r,A,P; /*declare variables*/
//ask user for the choice of shape
printf("Welcome to Cartesian geometry! Please choose your shape by entering its number:\n");
printf("Rectangle -> enter 1\n");
printf("Circle -> enter 2\n");
printf("Triangle -> enter 3\n");
printf("Exit Program -> enter 4\n");
scanf("%d", &choice);
if (choice==1) /*The User enters the Cartesian coordinates of the rectangle*/
{
printf("Please enter the coordinates for the rectangle from bottom left to right, then top right to left");
printf("\nenter value for x1:");
scanf("%f", &x1);
printf("enter value for y1:");
scanf("%f", &y1);
printf("enter value for x2:");
scanf("%f", &x2);
printf("enter value for y2:");
scanf("%f", &y2);
printf("enter value for x3:");
scanf("%f", &x3);
printf("enter value for y3:");
scanf("%f", &y3);
printf("enter value for x4:");
scanf("%f", & x4);
printf("enter value for y4:");
scanf("%f", &y4);
side_1=pow(pow((x2-x1), 2) + pow((y2-y1), 2), .5); /*Calculate the length of side 1 using the distance formula*/
side_2=pow(pow((x3-x2), 2) + pow((y3-y1), 2), .5); /*Calculate the length of side 2 using the distance formula*/
A= side_1*side_2;
printf("The Area is %f", A); /* Display the Area and Perimeter*/
P=2*(side_1+side_2);
printf("The Perimeter is %f", P);
}
if (choice==2)
{
printf("You have chosen a cirlce, please enter the coordinates of the centre, then a point on the circle\n"); /* User enters the coordinates of the circle*/
printf("enter value for x1\n");
scanf("%f", &x1);
printf("enter value for y1\n");
scanf("%f", &y1);
printf("enter value for x2\n");
scanf("%f", &x2);
printf("enter value for y2\n");
scanf("%f", &y2);
r=pow(pow((x2-x1), 2) + pow((y2-y1), 2), .5); /* Calculate radius using the distance formuala*/
A=3.141*(pow(r, 2));
printf("\n the area is %f", A); /* Display Area and Perimeter*/
P=2*3.141*r;
printf("\n the perimeter is %f", P);
}
if (choice==3)
{
printf("You have chosen a triangle, please enter in the coordinates of its vertices from bottom left to right, then top."); /*User enters coordiantes for the triangle*/
printf("\nEnter value for x1");
scanf("%f", &x1);
printf("\nplease enter value for y1:");
scanf("%f", &y1);
printf("\nplease enter value for x2:");
scanf("%f", &x2);
printf("\nplease enter value for y2:");
scanf("%f", &y2);
printf("\nplease enter value for x3:");
scanf("%f", &x3);
printf("\nplease enter value for y3:");
scanf("%f", &y3);
side_1=pow(pow((x2-x1), 2) + pow((y2-y1), 2), 0.5); /*Calculate length of side 1*/
side_2=pow(pow((x3-x2), 2) + pow((y3-y2), 2), 0.5); /*Calculate length of side 2*/
side_3=pow(pow((x3-x1), 2) + pow((y3-y1), 2), 0.5); /*Calculate length of side 3*/
A=0.25*pow(pow(pow(side_1, 2) + pow(side_2, 2) + pow(side_3, 2), 2) - 2*(pow(side_1, 4) + pow(side_2, 4) + pow(side_3, 4)), 0.5); /*Area of triangle using Heron's formula*/
printf("the area is %f", A); /* Display the Area and Perimeter*/
P= side_1 + side_2 + side_3;
printf("the perimeter is %f", P);
}
if (choice==4) /* User can chooses to exit*/
return(0);
}