**C program to find area of different shapes** is program to find area of different shapes such as triangle, circle, square etc.

In this tutorial we are going to see c program to find area of shapes, different formulas to find area of shapes. example to find area and program to calculate area of shapes in c.

**How to find area of shapes :**

To find area of shapes we use different mathematical formulas having for different shapes.

**Area of shapes formulas :**

Before doing programs let’s see different formula for calculating area of shapes.

**Lets see Area of shapes**

**1. Formula to calculate area of triangle :-**

Tringle has height and base and to find area of triangle we use formula

Area of triangle = (height * base)/2

**2. Formula to calculate area of rectangle :-**

Rectangle has length and breadth and to find area of rectangle we use formula

Area of rectangle = (length * breath)

**3. Formula to calculate area of square :-**

Square has a side with same dimension. So to find area of square we use formula

Area of square = (side * side)

**4. Formula to calculate area of circle :-**

Circle has a radius. So to find the area of circle we use formula

Area of circle = pi * radius * radius

**5. Formula to calculate area of parallelogram :-**

Parallelogram has a base and height. To calculate area of parallelogram we use formula

Area of parallelogram = (base * height)

**Logic to calculate area of shapes :**

First we take a input from user about their choice shape that which user want to find a area. We write a switch case with different case to find area of different shapes . Depending on user input find area of shape.

**Concept you need to know from c :**

1. Input and output in c

**Algorithm to calculate area of shapes :**

1. Start

2. Write a switch case to find area of different shapes.

3. Take a choice shape from user which user want to find area

4. In every switch case find area of different shapes.

5. Print area of shape in case

6. End

**C program to find area of different shapes**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
#include<stdio.h> #include<math.h> main() { int choice; printf("Enter\n1 for Triangle\n2 for Square\n3 for Circle\n4 for Rectangle\n5 for Parallelogram\n"); scanf("%d",&choice); switch(choice) { case 1: { int a,b,c; float s,area; printf("Enter sides of triangle\n"); scanf("%d %d %d",&a,&b,&c); s=(float)(a+b+c)/2; area=(float)(sqrt(s*(s-a)*(s-b)*(s-c))); printf("Area of Triangle with sides %d,%d,%d is %f\n",a,b,c,area); break; } case 2: { float side,area; printf("Enter Sides of Square\n"); scanf("%f",&side); area=(float)side*side; printf("Area of Square is %f\n",area); break; } case 3: { float radius,area; printf("Enter Radius of Circle\n"); scanf("%f",&radius); area=(float)3.14159*radius*radius; printf("Area of Circle with radius %f is %f\n",radius,area); break; } case 4: { float len,breadth,area; printf("Enter Length and Breadth of Rectangle\n"); scanf("%f %f",&len,&breadth); area=(float)len*breadth; printf("Area of Rectangle is %f\n",area); break; } case 5: { float base,height,area; printf("Enter base and height of Parallelogram\n"); scanf("%f %f",&base,&height); area=(float)base*height; printf("Enter area of Parallelogram is %f\n",area); break; } default: { printf("Invalid Choice\n"); break; } } } |

**Output :**

**Explanation :**

**1. First include header files we require stdio.h and math.h in program.**

**2. Write main function.**

**3. Declare variable**

Choice => to store the user choice (shape)

a,b,c → used for triangle sides which are taken from user(local variables which work only inside that particular case i.e. “case 1”)

side,area→ used for calculations of Square(local variables which work only inside that particular case i.e. “case 2”)

radius,area→ used for calculations of Circle(local variables which work only inside that particular case i.e. “case 3”)

length,breadth,area→ used for calculations of Rectangle(local variables which work only inside that particular case i.e. “case 4”)

base,height,area→ used for calculations of Parallelogram(local variables which work only inside that particular case i.e. “case 5”).

**4. We take a user input that for which shape user want to find area.**

printf(“Enter\n1 for Triangle\n2 for Square\n3 for Circle\n4 for Rectangle\n5 for Parallelogram\n”);

scanf(“%d”,&choice);

**Example** : suppose choice = 4 (to find area of rectangle)

**5. Next is switch case**

switch(choice){

we pass the user input a choice to switch case. The switch case will search case with a choice if any case matches with input then that case is executed.

**6. In our example with choice = 4**

switch(4) it check for a matching case.

First it checks case 1 which not matches so it move to next .

2 which not matches so move to next case.

3 which also not matches and move to next case.

4 checked now this case matches with the input.

**7. Case 4 matched so case 4 will get executed**

This case is to find area of rectangle. In this case we take a input length and breath of the rectangle.

printf(“Enter Length and Breadth of Rectangle\n”);

scanf(“%f %f”,&len,&breadth)

**Example** : len = 10 * 20

**8. After taking input we find area of rectangle**

area=(float)len*breadth => area = 200.00

**9. Now print area of rectangle**

printf(“Area of Rectangle is %f\n”,area) =>Area of Rectangle is 200.00

**10. At the end of the case we write a break keyword to skip remaining case execution.**

break;

**11. So the switch case terminated.**

**12. At the end we have area of shape.**

## Leave a Reply