
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Simple Calculator Program Using C Language
A calculator is a simple tool that helps us to calculate mathematical operations easily and quickly. A basic calculator can perform simple arithmetic operations like subtraction, addition, multiplication, and division.
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: Basic Usage
Following is the C program for the calculator. It specifies the user to enter an operator(+, -, *, /) and two operands. By using the Switch Case it performs the arithmetic operation on the operands and stores the result ?
#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; }
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
Advertisements