This is a C Program to simulate a simple calculator.

This C Program simulates a simple calculator.

This program performs arithmatic operations like addtion, subraction, multiplication & division. Assume that the 2 numbers a & b are given. For the given element we need to perform addition, subtraction, multiplication & division.

Here is source code of the C program which simulates a simple calculator. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C program to simulate a simple calculator to perform arithmetic * operations like addition, subtraction, multiplication and division */ #include <stdio.h> void main() { char operator; float num1, num2, result; printf("Simulation of a Simple Calculator\n"); printf("*********************************\n"); printf("Enter two numbers \n"); scanf("%f %f", &num1, &num2); fflush(stdin); printf("Enter the operator [+,-,*,/] \n"); scanf("%s", &operator); switch(operator) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; break; default : printf("Error in operationn"); break; } printf("\n %5.2f %c %5.2f = %5.2f\n", num1, operator, num2, result); }

In this C program reading two integers and operator symbol using ‘num1’, ‘num2’ and ‘operator’ variables respectively.

Switch case statement is used to perform arithmetic operations like addition, subtraction, multiplication and division in each case. If the operator symbol does not match in the switch case statement then execute the default statement. Print the statement as “Error in operation”.

$ cc pgm.c $ a.out8 Simulation of a Simple Calculator ********************************* Enter two numbers 2 3 Enter the operator [+,-,*,/] + 2.00 + 3.00 = 5.00 $ a.out Simulation of a Simple Calculator ********************************* Enter two numbers 50 40 Enter the operator [+,-,*,/] * 50.00 * 40.00 = 2000.00 $ a.out Simulation of a Simple Calculator ********************************* Enter two numbers 500 17 Enter the operator [+,-,*,/] / 500.00 / 17.00 = 29.41 $ a.out Simulation of a Simple Calculator ********************************* Enter two numbers 65000 4700 Enter the operator [+,-,*,/] - 65000.00 - 4700.00 = 60300.00

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms