C Program to Make a Simple Calculator Using Switch Case
C is a programming language designed and developed by Dennis Ritchie in 1972. It is an old programming language that is still used in various applications. C language is considered the template of all programming languages because the new languages which came after the C language are based on it. The syntax and the declarations of these languages are mostly based on the C language.
The features offered by this language are easily understandable and portable, faster execution speeds, less compiling time, and clear syntax.
The basic syntax of C language is:
#inlcude<stdio.h>
int main()
{
printf(“HELLO WORLD!!”);
return 0;
}
This program gives an output: HELLO WOLRD!!
Now let us understand the program and analyze it line by line. The basic C program contains the following modules.
1. Header files
This is the first line in the C program. This contains an extension .h which contains the declarations predefined in the language. In the above example, the header file is stdio.h. This defines the basic input and output functions for the program.
There are several other declarations used such as stdlib.h, math.h, string.h, and so on. apart from the above-mentioned header file.
2. Main method
This is the second line in the program which defines the main function of the program. This acts as the starting point for the C program; the int here is declared for the return type of the main() function. This returns an output of the given program.
3. Body of the program
This is the main component of the C program where the line of code exists. This contains the statements to be executed, and the body of the C program is in curly braces {}. The statements contain various types like printf(). These statements are used to print the text in the output stage. These statements are declared by a semicolon (;) The semicolon is considered the declaration of the statements.
4. Return statement
This is used to return the values and the statements declared in the main body of the C program. The return statement returns the statements from the main() function. This terminates the execution of the C program.
To execute the above program, we need a code editor or a notepad to enter the program. The file must be saved with a .c extension which is used to compile the program for C language.
The execution of the program is different for various Operating systems; for Windows, we can use text editors or designated IDEs to write and compile the code. Some of the most used Integrated Development Environments (IDE) are Code blocks and Dev C++.
For Linux, we use a separate process; we use the GCC compiler, which is built into the Linux operating system. This is used to compile the program in C.
C language is used to develop various technologies and applications such as Gaming, OS development, Software development, Database, and many more.
C language contains different functionalities to make the program more interactive and understandable.
These include conditional and looping statements, each of which serves its functionalities. Among these statements is one of the important ones known as the switch statement; this statement is used as an alternative for if and else statements. The purpose of the switch statement is to test the conditions for the given value and produce an output; the switch statement contains multiple cases where each case is given a unique numbering and executed one after another. Each case has one break statement, which is used to terminate the execution of the current working switch case.
The syntax for the switch statement is:
switch(expression)
{
case 1: statement(s);
break;
case 2: statement(s);
break;
*We can have multiple numbers of cases*
default: statement(s);
}
Here a default condition gives an output when none of the cases works.
Let us understand the following switch statements with an example to make a calculator:
Example:
#include <stdio.h>
int main()
{
int a, b;
char ch;
printf("Enter your choice (1,2,3,4):\n");
scanf("%c", &ch);
printf("Enter two numbers:\n");
scanf("%d%d", &a, &b);
switch (ch)
{
case '1':
printf("Addition of the numbers is:%d \n", a + b);
break;
case '2':
printf("Subtraction of the numbers is:%d \n", a - b);
break;
case '3':
printf("Multiplication of the numbers is:%d \n", a * b);
break;
case '4':
printf("Division of the numbers is:%d \n", a / b);
break;
}
return 0;
}
Output:
The output for the following program will be:
Enter your choice (1,2,3,4):
1
Enter two numbers:
20 25
Addition of the numbers is:45
Enter your choice (1,2,3,4):
2
Enter two numbers:
35 25
Subtraction of the numbers is:10
Enter your choice (1,2,3,4):
3
Enter two numbers:
5 6
Multiplication of the numbers is:30
Enter your choice (1,2,3,4):
4
Enter two numbers:
30 5
Division of the numbers is:6
This program takes the input from the user and then returns the output. Here the inputs are two integer numbers, and the aim is to perform operations for the given numbers. Using the switch cases, this performs the operation based on the user input. Here the switch cases are assigned as per the given choice '1' for addition, '2' for subtraction, '3' for multiplication, and '4' for division. After the input, the cases are executed and give the correct output for the selected choice.