0% found this document useful (0 votes)
4 views

Simple calculator

The document contains a C program that simulates a simple calculator, allowing users to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus. Users input expressions without spaces, and the program processes the input and displays the result. The program continues to prompt for new expressions until the user chooses to quit.

Uploaded by

niranjanmv50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Simple calculator

The document contains a C program that simulates a simple calculator, allowing users to perform basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus. Users input expressions without spaces, and the program processes the input and displays the result. The program continues to prompt for new expressions until the user chooses to quit.

Uploaded by

niranjanmv50
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Write a C program to simulate a simple calculator.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int iChoice, iOperand1, iOperand2;
char cOperator;

for(;;)
{
printf("\nEnter the arithmetic expression(Do not add spaces in the
expression)\n");
scanf("%d%c%d", &iOperand1, &cOperator, &iOperand2);
switch(cOperator)
{
case ’+’: printf("\nResult = %d", iOperand1 + iOperand2);
break;

case ’-’: printf("\nResult = %d", iOperand1 - iOperand2);


break;

case ’*’: printf("\nResult = %d", iOperand1 * iOperand2);


break;

case ’/’: printf("\nResult = %g", (float)iOperand1 / iOperand2);


break;
case ’%’: printf("\nResult = %d", iOperand1 % iOperand2);
break;
}
printf("\nPress 1 to continue and 0 to quit : ");
scanf("%d", &iChoice);
if(0==iChoice)
{
break;
}
}
return 0;
}

/Output
=================================
***************************************
Enter the arithmetic expression
4+6
Result = 10
Press 1 to continue and 0 to quit : 1

Enter the arithmetic expression


2-9

Result = -7
Press 1 to continue and 0 to quit : 1

Enter the arithmetic expression


5*2

Result = 10
Press 1 to continue and 0 to quit : 1

Enter the arithmetic expression


4/5

Result = 0.8
Press 1 to continue and 0 to quit : 1

Enter the arithmetic expression


8/4

Result = 2

Press 1 to continue and 0 to quit : 1


Enter the arithmetic expression
15%4

Result = 3
Press 1 to continue and 0 to quit : 0
***************************************/

You might also like