Browse Products View Product Index | X Close Menu

# Simulate a simple calculator to perform arithmetic operations on integers

Code:
`/* Write a C program to simulate a simple calculator to perform    * * arithmetic operations like addition, subtraction,multiplication * * and division only on integers. Error message should be repoetrd * * if any attempt is made to divide by zero			   */#include <stdio.h>#include <conio.h>void main(){	char oper;			/* oper is an operator to be selected */	float n1, n2, result;clrscr();printf ("Simulation of a Simple Calculator\n\n");printf("Enter two numbers\n");	scanf ("%f %f", &n1, &n2);fflush (stdin);printf("Enter the operator [+,-,*,/]\n");	scanf ("%c", &oper);	switch (oper)   	{		case '+': result = n1 + n2;			  break;		case '-': result = n1 - n2;			  break;		case '*': result = n1 * n2;			  break;		case '/': result = n1 / n2;			  break;		default : printf ("Error in operation\n");			  break;	}printf ("\n%5.2f %c %5.2f= %5.2f\n", n1,oper, n2, result);}/*-----------------------------OutputSimulation of Simple CalculatorEnter two numbers3 5Enter the operator [+,-,*,/]+3.00 +  5.00=  8.00RUN2Simulation of Simple CalculatorEnter two numbers12.758.45Enter the operator [+,-,*,/]-12.75 -  8.45=  4.30RUN3Simulation of Simple CalculatorEnter two numbers12 12Enter the operator [+,-,*,/]*12.00 * 12.00= 144.00RUN4Simulation of Simple CalculatorEnter two numbers59Enter the operator [+,-,*,/]/5.00 /  9.00=  0.56------------------------------*/ `

You are here