
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to write a simple calculator program using C language?
Begin by writing the C code to create a simple calculator. Then, follow the algorithm given below to write a C program.
Algorithm
Step 1: Declare variables Step 2: Enter any operator at runtime Step 3: Enter any two integer values at runtime Step 4: Apply switch case to select the operator: // case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default: printf("
Invalid Operator "); Step 5: Print the result
Example
Following is the C program for calculator by using the Switch Case −
#include <stdio.h> int main(){ char Operator; float num1, num2, result = 0; printf("
Enter any one operator like +, -, *, / : "); scanf("%c", &Operator); printf("Enter the values of Operands num1 and num2
: "); scanf("%f%f", &num1, &num2); switch(Operator){ case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default: printf("
Invalid Operator "); } printf("The value = %f", result); return 0; }
Output
When the above program is executed, it produces the following result −
Enter any one operator: + Enter values of Operands num1 and num2: 23 45 The value = 68.000000
- Related Articles
- C/C++ program to make a simple calculator?
- Java Program to Make a Simple Calculator Using switch...case
- Golang Program to make a Simple Calculator using Switch Case
- Haskell Program to Make a Simple Calculator Using switch...case
- Menu Driven C++ Program for a Simple Calculator
- C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case
- Create a simple calculator using Java Swing
- How to build a simple GUI calculator using tkinter in Python
- Simple GUI calculator using Tkinter in Python
- Basic calculator program using C#
- Creating a Simple Calculator using HTML, CSS, and JavaScript
- Write a structure in local scope program using C language
- Write an example program on structure using C language
- Basic calculator program using Python program
- Basic calculator program using Java

Advertisements