0% found this document useful (0 votes)
44 views

Assignment 2 - Answerr PDF

This C++ program calculates the area of different shapes (square, rectangle, triangle, circle) by calling the function f_area and passing the appropriate parameters. The main program prompts the user to select a shape, inputs the required dimensions, calls f_area to calculate the area, and prints the result. It also allows the user to calculate multiple shapes by repeating the process until the user enters 'n' to quit.

Uploaded by

Lai Yen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Assignment 2 - Answerr PDF

This C++ program calculates the area of different shapes (square, rectangle, triangle, circle) by calling the function f_area and passing the appropriate parameters. The main program prompts the user to select a shape, inputs the required dimensions, calls f_area to calculate the area, and prints the result. It also allows the user to calculate multiple shapes by repeating the process until the user enters 'n' to quit.

Uploaded by

Lai Yen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Assignment 2 - Solution

#include <stdio.h>
void f_area (float x_f, float y_f, char select_f, float &area_f)
{
switch(select_f)
{
case ('s'):
area_f=x_f*x_f; break;
case ('r'):
area_f=x_f*y_f; break;
case ('t'):
area_f=0.5*x_f*y_f; break;
case ('c'):
area_f=x_f*x_f*3.14; break;
}
};
//---------------------main program-------------------------------------------int main()
{
char select,ch='y'; float x, y,area;
printf(" This program performed by:\n");
printf(" Name : Anas M Mzahm. \n ID
: PT20449.\n Section : CSEB 4A & 4B.\n");
printf("\n --------------------------\n");
while (ch!='n')
{
ch='n';
printf("\n Please choose what shape you want to calculate its area!\n");
printf("\n Enter S for Square.\n\n Enter R for Rectangle.\n);
printf(\n Enter T for Triangle.\n\n Enter C for Circle.\n");
printf("\n Choose: ");
scanf("%s",&select);
if (select =='s'||select =='S')
{
select='s';
printf("\n Please eneter the square side length in cm : ");
scanf("%f",&x);
f_area(x,y,select,area);
printf("\n The square area = %.1f cm\n",area);
}
else
if (select =='r'||select =='R')
{
select='r';
printf("\n Please enter the rectangle width in cm : ");
scanf("%f",&x);
printf("\n Please enter the rectangle length in cm : ");
scanf("%f",&y);
f_area(x,y,select,area);
printf("\n The rectangle area = %.1f cm\n",area);
}
else
if (select =='t'||select =='T')
{

5
10

4
1

4
1

select='t';
printf("\n Please enter the triangle base in cm : ");
scanf("%f",&x);
printf("\n Please enter the triangle height in cm : ");
scanf("%f",&y);
f_area(x,y,select,area);
printf("\n The triangle area = %.1f cm\n",area);
}

else
if (select =='c'||select =='C')
{
select='c';
printf("\n Please enter the circle radius in cm : ");
scanf("%f",&x);
f_area(x,y,select,area);
printf("\n The circle area = %.1f cm\n",area);
}
else
printf("\n You enter un-identified character!\n");
printf("\n Do you want to calculate another shape area?\n);
printf(\n Enter (Y for Yes) or (N for No): ");
scanf("%s",&ch);
}
printf("\n\n Thank you for using our program, have a nice day.\n\n \t\t\t Goodbye \n\n" );
return 0;
}

4
1

2
2

You might also like