**Introduction to Switch Case in C**

**Switch case** in the c programming language is a very important concept. In c programming, it is an alternative to an if-else ladder. In the switch case statement, you have multiple options and you can select one of them.

Also read: Switch Case in C

**Formulae Used in this C Program**

We need to calculate the area of the circle and triangle for this program using a switch case statement.

**Area of Circle**

`a = 3.14 * r * r`

In the above formula, a is area and r is radius of a circle.

**Area of Triangle**

We can calculate the area of the triangle in two ways. First, when we know only base and height and the other way is when you have been given values of three sides of the triangle. This formula is known as Heron’s formula.

`a = 1/2 * b * h`

Where a is an area, b is the base and h is the height of the triangle.

**Heron’s Formula**

First, calculate the perimeter of the triangle. Perimeter means the addition of all the sides. Then divide that perimeter by 2. Suppose we call it semi-perimeter (S).

```
S = (s1+s2+s3)/2
a = sqrt(S(S-a)(S-b)(S-c))
```

Here, S is semi-perimeter, s1, s2 and s3 are sides of the triangle and a is the area of a triangle. The sqrt means square root that we are going to use in this program.

Also read: LCM of Two Numbers in C

In this c program, we have to write a c program to calculate the area of circles and triangles using a switch case statement. So let’s see the output of this program first.

**Output**

There are three outputs, I am showing here. Because there are three choices for the user.

**Area of Circle:**

**Area of the triangle on the basis of base and height.**

**Heron’s Formula to Calculate Area of Triangle**

**Output Explanation using Switch Case**

In the above, I have given three expected outputs. Why 3? Because we have been given three options and those are:

```
1. Area of Circle
2. Area of Triangle(Base and Height)
3. Area of Triangle(All three Sides)
Select your Choice
```

These choices are for the user. He has to select any one choice from the above three choices. If he selects choice 1, then he will be asked to enter the radius. Because this choice is for calculating the **area of the circle**.

Also read: Floyd Triangle in C Programming

Similarly, if the user selects option 2 or 3 then he has to enter different values to calculate **the area of triangles**. One way to calculate the area of a triangle by using the formula **1/2 * base * height** and another way is to use **heron’s formula.**

Let’s see the c program first and then we will see the explanation.

**C Program to Calculate Area of Circle, Rectangle and Triangle using a switch statement**

```
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float area, radius, s1,s2,s3,base,height,sp;
int choice;
printf("1. Area of Circle\n");
printf("2. Area of Triangle(Base and Height)\n");
printf("3. Area of Triangle(All three Sides)\n");
printf("Select your Choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("You have chosen Area of Circle\n");
printf("Enter the radius\n");
scanf("%f",&radius);
area=3.14*radius*radius;
printf("Area of Circle is %.2f\n",area);
break;
case 2:
printf("You have chosen Area of Triangle(Base and Height)\n");
printf("Enter base and height\n");
scanf("%f%f",&base,&height);
area=(base*height)/2;
printf("Area of Triangle is %.2f\n",area);
break;
case 3:
printf("You have chosen Area of Triangle(All three Sides)\n");
printf("Enter values of all three sides\n");
scanf("%f%f%f",&s1,&s2,&s3);
sp=(s1+s2+s3)/2;
area=sqrt(sp*(sp-s1)*(sp-s2)*(sp-s3));
printf("Area of Triangle is %.2f\n",area);
break;
default:
printf("Sorry, Invalid Choice\n");
}
printf("Thank You\n");
return 0;
}
```

**Explanation**

I have already mentioned what formulae we are going to use in this program. But in this section, I will explain what is the logic behind this c program using the switch case statement in c.

**#include<math.h>**

I have included this header file because I am using one function i.e. sqrt(). The definition of this function is defined in this header file. Without this header file, this function will show an error and we will not get our output.

**Also Read: **Insert and Delete element in Array in C using switch case

**float area, radius, s1,s2,s3,base,height,sp;**

This is the variable declaration part. The variable area will store the area of the circle or area of a triangle, the radius will store radius from the user, s1, s2, s3 are sides of the triangle. The variable base and height will store base and height values.

All these variables are declared as the float.

**int choice;**

We are using a switch case statement, so we need one integer expression as per the syntax. On the basis of the value of this choice, the respective case will be executed. When the user enters his choice from the console, then that value is stored in this variable.

**switch(choice)**

This is the keyword switch that contains a choice variable. This switch has a body and in that body, there is a number of cases. As per our output, the user will decide, what he needs to calculate? Then he will enter the value and that value is stored in this choice variable.

**Also Read: **Palindrome in C using Pointers

If the value of the variable choice is 1, then case 1 will be executed, if 2 then case 2 will be executed and so on. But in this program, we have used only three cases. Case 1 for the area of a circle, case 2 for the area of a triangle with base and height and case 3 for again the area of a triangle, but this time using heron’s formula.

**case 1:**

In this case, we are asking the user to enter the value of radius. Because, in this case, we are going to calculate the area of the circle. We will use the above formula to calculate the area of the circle. Whatever, the value of the area is calculated, we will then display it on the console. You can see the output.

**break;**

This is the break keyword. This can be used in switch-case statements or any loop statement. Here, we are using this break keyword in a switch case statement. It indicates that the case is terminated and don’t execute further cases. Without this break statement, the case will not be terminated and we will not get our expected output.

**Also Read: **C Program to Display a String in Capital Letters

In this case 1, the area of the circle is calculated and we have finished our task. Without a break statement, case 2 will also execute that we don’t want. I hope you have understood the use of the break statement.

**case 2:**

If the user enters 2, that means he wants to calculate the area of a triangle. But he has the value of base and height. So, in this case, the area of a triangle is calculated and that will be displayed on the console.

**case 3:**

But, if the user enters option 3, that means he wants to calculate the area of a triangle using heron’s formula. I have explained this formula in the above section. So we have used this formula here.

**default:**

This is an optional keyword. But I will recommend you to use this keyword. Why? When no case is matched with the given choice, then this default part will be executed. This is just like else statement. When if the statement is not executed then else part will be executed.

**Also Read: **Operators in C with Detailed Explanation

There is a possibility that a user can enter any number other than the choices that you have given. In such a situation, this is the best use of the default keyword.

So, this was all about the c program to calculate the area of circle and rectangle using a switch case statement. I hope you have understood this program. So, thank you very much for reading this article.

**Important Programs**

- Copy the Contents of One File into Another File
- Print All The Numbers In a Given Range
- C Program to Display a String in Capital Letters
- Remove Zeros from a number
- C Program to Find Camel Case without using Library Function
- Display Middle Row and Column of Matrix
- C Program to Remove Vowels from Even Position in a String

**FAQs**

**Is break needed in every switch case?**

No, it is optional. After executing a case, the break will help you to come out of the switch statement. In the absence of a break keyword, the next case will be executed. See the following example:**switch with a break statement**int main()

{

int a=1;

switch(a)

{

case 1:

printf(“This is case 1”);

break;

case 2:

printf(“This is case 2”);

}

return 0;

}

**Output**

This is case 1

**switch without using the break statement**

int main()

{

int a=1;

switch(a)

{

case 1:

printf(“This is case 1”);

case 2:

printf(“This is case 2”);

}

return 0;

}

**Output**

This is case 1

This is case 2

In this example, the case is not terminated here.