Calculators have been a part of our daily routine since childhood. We use them for various purposes like arithmetic calculations, trigonometric calculations, and much more. To carry out these calculations more systematically, we use calculators. However, have you ever thought about creating a simple calculator on your own using a switch statement in C programming language?

In this article, you will learn how to create a simple calculator that can perform basic arithmetic operations like addition, subtraction, multiplication, and division as well as calculating the percentage.

Switch statement in C language

Before we dive into creating our calculator, let's first understand the switch statement in C language. The switch statement is a control statement that allows you to test the value of an expression against various values. The syntax of a switch statement is as follows:

```
switch(expression) {
case value1:
// statement block
break;
case value2:
// statement block
break;
...
default:
// statement block
break;
}
```

Here, 'expression' represents the value that needs to be tested against various 'case' values. The 'case' values represent the different values that the expression can take. The 'break' keyword is used to break out of the switch statement. If none of the 'case' values matches the 'expression' value, then the 'default' statement block is executed.

Creating a simple calculator using switch statement in C language

Now that we have a basic understanding of switch statement let's move on to creating our calculator. Follow the below steps to create a simple calculator that can perform basic arithmetic operations like addition, subtraction, multiplication, and division as well as calculating the percentage:

Step 1: Declare variables

Declare the variables that we will be using in our calculator. We need three variables to perform an arithmetic operation. The variables are 'num1', 'num2', and 'result'. We will declare these variables as follows:

```
int num1, num2, result;
```

Step 2: Input values

Input the values of 'num1' and 'num2' variables from the user. We can do that using the 'scanf' function as shown below:

```
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
```

Step 3: Create a switch statement

Create a switch statement that will perform an arithmetic operation as per the user's input. The switch statement will have four 'case' statements, one for each arithmetic operation. We will also include a 'default' statement that will be executed if none of the arithmetic operations is selected.

```
switch(operation) {
// addition
case '+':
result = num1 + num2;
printf("The sum is %d", result);
break;
// subtraction
case '-':
result = num1 - num2;
printf("The difference is %d", result);
break;
// multiplication
case '*':
result = num1 * num2;
printf("The product is %d", result);
break;
// division
case '/':
result = num1 / num2;
printf("The quotient is %d", result);
break;
// default statement
default:
printf("Invalid operator!!");
break;
}
```

Step 4: Create a switch statement for percentage calculation

Create another switch statement to calculate the percentage of a given number. The switch statement will have only one 'case' statement for calculating the percentage and a 'default' statement that will be executed if none of the percentage calculations is selected.

```
switch(operation) {
// percentage calculation
case '%':
result = (num1 * num2) / 100;
printf("%d%% of %d is %d", num2, num1, result);
break;
// default statement
default:
printf("Invalid operator!!");
break;
}
```

The complete code for creating a simple calculator using switch statement in C language is given below:

```
#include <stdio.h>
int main() {
int num1, num2, result;
char operation;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter an operator (+, -, *, / or %): ");
scanf(" %c", &operation);
switch(operation) {
// addition
case '+':
result = num1 + num2;
printf("The sum is %d", result);
break;
// subtraction
case '-':
result = num1 - num2;
printf("The difference is %d", result);
break;
// multiplication
case '*':
result = num1 * num2;
printf("The product is %d", result);
break;
// division
case '/':
result = num1 / num2;
printf("The quotient is %d", result);
break;
// default statement
default:
printf("Invalid operator!!");
break;
}
printf("
");
printf("Enter an operator '%%' to calculate the percentage: ");
scanf(" %c", &operation);
switch(operation) {
// percentage calculation
case '%':
result = (num1 * num2) / 100;
printf("%d%% of %d is %d", num2, num1, result);
break;
// default statement
default:
printf("Invalid operator!!");
break;
}
return 0;
}
```

Conclusion

In this article, we have learned how to create a simple calculator that can perform basic arithmetic operations like addition, subtraction, multiplication, and division as well as calculating the percentage. We have also learned how to use switch statement in C language to perform these operations. By now, you should be able to create your own calculator using switch statement in C language.

I apologize, but as an AI language model, I do not have information on the previous topic(s) you are referring to. If you can provide me with more details, I will be more than happy to help you to the best of my abilities.

## Popular questions

Sure, here are five simple questions and their corresponding answers on creating a simple calculator using a switch statement in the C programming language:

Q1. What is a switch statement?

A1. A switch statement is a control statement in C language that allows you to test the value of an expression against various values.

Q2. How do you create a switch statement in C?

A2. To create a switch statement in C, you need to write the keyword "switch" followed by the expression you want to test. Then, you add a series of "case" statements that correspond to different values of the expression.

Q3. What arithmetic operations can a simple calculator perform using switch statement in C?

A3. A simple calculator using a switch statement in C can perform basic arithmetic operations such as addition, subtraction, multiplication, and division, as well as calculating the percentage.

Q4. How do you declare variables in C language?

A4. To declare variables in C language, you need to specify the data type followed by the variable name. For example, "int num1" declares an integer variable named "num1".

Q5. What is the syntax to perform arithmetic operations using switch statement in C?

A5. To perform arithmetic operations using a switch statement in C, you need to write the keyword "switch" followed by the expression you want to test. Then, you add a series of "case" statements that correspond to different values of the expression. Inside each case statement, you write the logic for the arithmetic operation you want to perform. You also need to add a "default" statement that is executed if none of the "case" statements match the expression.

### Tag

Computation