Home » Calculator Program in C

Calculator Program in C

by Online Tutorials Library

Calculator Program in C

In this topic, we will discuss how we write a calculator program in the C programming language. A Calculator is a small electronic device used to perform various arithmetic operations like addition, subtraction, multiplication, division, percentage, etc. It makes our calculations easier and faster. It is a portable device that can use anywhere to perform simple mathematical operations. We use a scientific or sophisticated calculator in some situations, where we need to solve complex calculations like trigonometry functions, exponential operators, degrees, radians, log functions, hyperbolic functions etc. Let’s discuss the various ways to create a calculator program in the C language.

Calculator Program in C

Algorithm of Calculator Program

Step 1: Declare local variables n1, n2, res, opt. For example, where n1 and n2 take two numeric values, res will store results and opt variable define the operator symbols.

Step 2: Print the Choice (Addition, Subtraction, multiplication, division, etc.

Step 3: Enter the Choice

Step 4: Takes two numbers, n1 and n2

Step 5: Switch case jump to an operator selected by the user

Step 6: Store result into res variable.

Step 7: Display the operation result

Step 8: Exit from the program.

Different ways to create a Calculator Program in C

Following are the different ways to write a Calculator Program in the C language.

  1. Calculator Program in C using the switch statement
  2. Calculator Program in C using if else if statement
  3. Calculator Program in C using do-while loop and switch statement
  4. Calculator Program in C using function and switch statement

Example 1: Calculator Program in C using the switch statement

Let’s write a program to create a Calculator program using switch statement

program.c

Output:

Calculator Program in C

Example 2: Calculator Program in C using if else if statement

Let’s consider an example to write a simple Calculator program in C using if else if statement.

program2.c

Output:

Calculator Program in C

Example 3: Calculator Program in C using do while loop and switch statement

Let’s create a Calculator program using do while loop and switch case statement in C

program3.c

Output:

Calculator Program in C

Example 4: Calculator Program in C using function and switch statement

Let’s create a Calculator program using function and switch case statement in C

program4.c

Output:

Calculator Program in C


Next TopicCalloc in C

You may also like