KEMBAR78
Lab Program1 | PDF
0% found this document useful (0 votes)
5 views4 pages

Lab Program1

The document outlines a C program that simulates a simple calculator with operations for addition, subtraction, multiplication, division, and modulus. It includes an algorithm detailing the steps for user input and operation selection, as well as a flowchart and the complete C code implementation. The program handles invalid choices and division by zero gracefully.
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)
5 views4 pages

Lab Program1

The document outlines a C program that simulates a simple calculator with operations for addition, subtraction, multiplication, division, and modulus. It includes an algorithm detailing the steps for user input and operation selection, as well as a flowchart and the complete C code implementation. The program handles invalid choices and division by zero gracefully.
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/ 4

Program 1:

C program to implement Simulation of a Simple Calculator.


Algorithm

Step-1: Start
Step-2: Read 2 inputs a and b
Step-3: Display the options
Step-4: Read the input choice
Step-5: If Choice is
1 then execute step 6
2 then execute step 7
3 then execute step 8
4 then execute step 9
5 then execute step 10
6 then exit
Other than above mentioned choice than execute step 12
Step-6: Addition of a and b and print res
Step-7: Subtraction of a and b and print res
Step-8: Multiplication of a and b and print res
Step-9: Division of a and b and print res
Step -10: Modulus of a and b and print res
Step -11: Exit the program
Step-12: Invalid choice
Step-13: Stop

Flow Chart
Program:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,res;
char op;
printf(" Simulation of Calculator\n");
printf(" +: Addition\n -: Subtraction\n *: Multiplication\n /:.Division\n
%:.Modulus\n “);
printf("Enter the values of a operator b: \n");
scanf("%d%c%d",&a,&op,&b);

switch(op)
{
case '+': res=a+b;
break;
case '-': res=a-b;
break;
case '*':res=a*b;
break;
case '/':if( b==0)
{
printf(" Cannot divide\n");
exit(0);
}
else
res=a/b;
break;
case '%':res=a%b;
break;
default : printf(" Invalid operator.\n");
exit(0); }
printf("The result of %d %c %d = %d", a,op,b,res);
return 0;
}

You might also like