Get the Area of Shapes

A tutorial code how to get the areas of Rectangle, Square, Triangle, Square, Circle, Hexagon and also I included the Monthly Wage, Half Yearly Wage and Fiscal Wage Computation of an employee, the structure is simply filter the assigned letter of each computation using IF and ELSE IF Selection Structure, I used a single WHILE Loop to repeat the statement based on the condition..


OUTPUT


#include <stdio.h>
int main()
{
    int height, width, base;
float r, side, segment;
float cosTheta = 0.866025;
const float pi = 3.14159;
    char select, ans;
    float h, w;
int days, hours;
ans = 'y';
while (ans=='y' || ans=='Y')
 {
    system("color A");
printf("SELECT YOUR OPTION\n\n");
printf("[ A ]\tHow to get the area of SQUARE\n");
printf("[ B ]\tHow to get the area of RECTANGLE\n");
printf("[ C ]\tHow to get the area of TRIANGLE\n");
printf("[ D ]\tHow to get the area of CIRCLE\n");
printf("[ E ]\tHow to get the area of HEXAGON\n");
printf("[ F ]\tHow to get the wage of employee\n");
printf("[ X ]\tTerminate Program\n\n\n");
scanf(" %c", &select);
if(select != 0)
{
//validate the select condition
if(select=='A'||select=='a')
{
printf("SQUARE - COMPUTATION OF AREA\n");
printf("\nEnter a value of side of square: ");
    scanf("%f", &side);
    //How to get the area of square, Area = side of square * 2
    printf("\n\n");  
    printf("The Area of square is: %f", side * 2);
    printf("\n\n");
}
else if(select=='B'||select=='b')
{
printf("RECTANGLE - COMPUTATION OF AREA\n");
printf("Enter a value for height of a rectangle: ");
    scanf("%d", &height);
printf("Enter a value for width of a rectangle: ");
    scanf("%d", &width);
    //How to get the area of rectangle
    printf("\n\n");  
    printf("The Area of rectangle is: %d", height * width);
    printf("\n\n");
}
else if (select=='C'||select=='c')
{
printf("TRIANGLE - COMPUTATION OF AREA\n");
printf("Enter a value for height of a triangle: ");
    scanf("%d", &height);
printf("Enter a value for base of a triangle: ");
    scanf("%d", &base);  
    //How to get the area of triangle, Area = (0.5 * (base * height))  
    printf("\n\n");  
    printf("The Area of triangle is: %f", 0.5 * (base * height));
    printf("\n\n");
}
else if(select=='D'||select=='d')
{
printf("CIRCLE - COMPUTATION OF AREA\n");
printf("Enter a value for radius of a circle: ");
    scanf("%f", &r);     //How to get the area of circle, Area = (pi * (radius * 2))  
    printf("\n\n");  
    printf("The Area of circle is: %f", (pi * (r * r)));
    printf("\n\n");
}
else if(select=='E'||select=='e')
{
printf("HEXAGON - COMPUTATION OF AREA\n");
printf("Enter a value for side of hexagon: ");
    scanf("%f", &side);
        segment=side*cosTheta;
        //How to get the area of hexagon  
    printf("The Area of hexagon is: %f", 6.0 * side * segment / 2);
    printf("\n\n");
}
else if(select=='F'||select=='f')
{
printf("WAGE - COMPUTATION OF WAGE\n");
printf("How many hours did you work a day: ");
    scanf("%d", &hours);
    printf("How many days did you work a week: ");
  scanf("%d", &days);
      //How to compute the wage of employee
    printf("\nMonthly wage is: Php %d", (hours * 60) * (days * 4));
    printf("\nHalfyearly wage is: Php %d", 6 * (hours * 60) * (days * 4));
    printf("\nFiscal wage is: Php %d", 12 * (hours * 60) * (days * 4));         printf("\n\n");
}
else if(select=='X'||select=='x')
{
printf("\nThank you for using this program!\n");
exit(0);
}
else
{
printf("Invalid Input.\n"); }
}
    printf("Would you like to try again? Y/N\n");
    scanf(" %c", &ans);
 }
    return 0;
}
Get the Area of Shapes Get the Area of Shapes Reviewed by code-dev on 12:06 PM Rating: 5

No comments:

Powered by Blogger.