101k views
1 vote
C programming

You are to write a program which will have the following Functions:
.getAreaRectangle
.getArea Triangle
.getAreaCircle
getPerimeterRectangle
.getPerimeter Triangle
• getPerimeterCircle

You will first need to ask the user if they want to find the Area or the Perimeter.

Then you will ask of which shape (Rectangle, Triangle, Circle).
Finally, based on the input of the user, you will call the Function, passing all necessary variables to the function. The function will
return the answer and you will then display the correct answer on the screen.

This is going to be a long program!! It will mainly test your ability to use Condition statements and Functions. You can use a switch
for the menu if you want.

User Radouane
by
8.1k points

1 Answer

2 votes

Answer:

Here's an example program in C that implements the requirements

#include <stdio.h>

#include <math.h>

// Function prototypes

double getAreaRectangle(double width, double height);

double getAreaTriangle(double base, double height);

double getAreaCircle(double radius);

double getPerimeterRectangle(double width, double height);

double getPerimeterTriangle(double side1, double side2, double side3);

double getPerimeterCircle(double radius);

int main() {

char shapeType;

char calcType;

// Ask user if they want to find the area or perimeter

printf("Do you want to find the area or perimeter? (a/p): ");

scanf("%c", &calcType);

// Ask user which shape they want to calculate for

printf("Which shape do you want to calculate for? (r/t/c): ");

scanf(" %c", &shapeType);

double result;

switch (shapeType) {

case 'r': // Rectangle

double width, height;

printf("Enter the width and height of the rectangle: ");

scanf("%lf %lf", &width, &height);

if (calcType == 'a') {

result = getAreaRectangle(width, height);

printf("The area of the rectangle is %.2lf\\", result);

} else if (calcType == 'p') {

result = getPerimeterRectangle(width, height);

printf("The perimeter of the rectangle is %.2lf\\", result);

}

break;

case 't': // Triangle

double base, height_t, side1, side2, side3;

printf("Enter the base and height of the triangle: ");

scanf("%lf %lf", &base, &height_t);

printf("Enter the three sides of the triangle: ");

scanf("%lf %lf %lf", &side1, &side2, &side3);

if (calcType == 'a') {

result = getAreaTriangle(base, height_t);

printf("The area of the triangle is %.2lf\\", result);

} else if (calcType == 'p') {

result = getPerimeterTriangle(side1, side2, side3);

printf("The perimeter of the triangle is %.2lf\\", result);

}

break;

case 'c': // Circle

double radius;

printf("Enter the radius of the circle: ");

scanf("%lf", &radius);

if (calcType == 'a') {

result = getAreaCircle(radius);

printf("The area of the circle is %.2lf\\", result);

} else if (calcType == 'p') {

result = getPerimeterCircle(radius);

printf("The perimeter of the circle is %.2lf\\", result);

}

break;

default:

printf("Invalid shape type entered.\\");

break;

}

return 0;

}

double getAreaRectangle(double width, double height) {

return width * height;

}

double getAreaTriangle(double base, double height) {

return 0.5 * base * height;

}

double getAreaCircle(double radius) {

return M_PI * radius * radius;

}

double getPerimeterRectangle(double width, double height) {

return 2 * (width + height);

}

double getPerimeterTriangle(double side1, double side2, double side3) {

return side1 + side2 + side3;

}

double getPerimeterCircle(double radius) {

return 2 * M_PI * radius;

}

~ In this program, the main() function prompts the user for ~

User Sy
by
7.6k points