In this post ,I will let you know how to make Area calculator. So, without any further delay let's get started .
Code:
#include <stdio.h>
int main()
{
char Shape;
float radius, side, length, breadth, base, height;
float areaC, areaS, areaR, areaT;
printf("Enter shape symbol for which you want Area(Circle=C,Square=S,Rectangle=R,Triangle=T)\n");
scanf("%c", &Shape);
if (Shape == 'c' || Shape == 'C')
{printf("Enter radius of circle\n");
scanf("%f", &radius);
areaC = 3.14 * radius * radius;
printf("Area of circle=%f\n", areaC);
}
else if (Shape == 'S' || Shape == 's')
{printf("Enter side of Square\n");
scanf("%f", &side);
areaS = side * side;
printf("Area of square=%f\n", areaS);
}
else if (Shape == 'R' || Shape == 'r')
{printf("Enter Length and Breadth of Rectangle \n");
scanf("%f %f", &length, &breadth);
areaR = length * breadth;
printf("Area of Rectangle=%f\n", areaR);
}
else if (Shape == 'T' || Shape == 't')
{printf("Enter Base and Height of Triangle\n");
scanf("%f %f", &base, &height);
areaT = 0.5 * base * height;
printf("Area of Triangle=%f\n", areaT);
}
return 0;
Logic:
Now let us first understand how algorithm for this .Here we make calculator only for four Shapes i.e. Circle ,Square ,Rectangle and Triangle .
For that we must know formula of all these .Below are formula for these:
Circle: Area=pi*radius*radius ,pi=3.14
Square: Area=Side*Side
Rectangle: Area=Length*Breadth
Triangle : Area=1/2*Base *Height
Know more about if-else. After this its time to understand each and everything about this program ,so that you don't left any doubt after reading this .First we declare variable Shape of type character and some float variables radius for circle ,side for square ,length and breadth for Rectangle and for Triangle base and height. And other variables areaC for Circle ,areaS for square ,areaR for rectangle and areaT for triangle of type float . Now we ask user to enter just symbol of shape C for Circle and R for rectangle , S for Square and T for Triangle . And then we use if (Shape =='C' || Shape =='c') ,that means we check if it is equal to C (capital letter ) or in c(small letter) then execute it for Circle .Here || is OR operator means if any of them is true then execute code .Now continue it printf() inside if gets executed and ask for Radius from user .Then he enters radius and program work and print area of Circle. And this work for all Square , Rectangle and Triangle.
Output:
Here you can see output ,it works as we think .It take symbol of Shape and then ask for Requirements to calculate its area. And give us correct output .
I hope you might find this helpful in your work.
Thanks for reading
No comments:
Post a Comment