Home

Calculator C Program Using Switch Case

Posted by Rashid Sharafat on Jun 26 2010

Calculator C program performs addition, subtraction, multiplication and division using switch case makes this program simple and errors free. I made it by using Switch Case. This Program can also made by Using Nested IF Else Conditions. But Nested IF Else makes it More Complicated. Switch case makes a program simple and Errors can ab detected easily.

First, Program will give you a Menu to choose the Operation. As You select the Operation, then You have to Put the values. The Drawback of this Program is that, it just for 2 Numbers. So You can enhance this program by applying a suited Logic.
I made it for Beginners not for Experts. I am using Turbo C++ Compiler.

Calculator C Program Output:


Calculator C Program Source Code:


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,sum,choice;
float div;
puts("***** Calculator *****");
cout<<"\nFor Addition - Press 1 \nFor Subtraction - Press 2 \nFor Multiplication - Press 3 \nFor Division - Press 4 \n";
choice=getch();
switch (choice)
{
case '1':
puts("\n\nAddition Panel");
puts("Enter 1st Value");
cin>>i;
puts("\nEnter 2nd Value");
cin>>j;
sum=i+j;
cout<<"\n\nThe Sum of Your Values Is "<<sum;
break;
case '2':
puts("\n\nSubtraction Panel");
puts("Enter 1st Value");
cin>>i;
puts("\nEnter 2nd Value");
cin>>j;
sum=i-j;
cout<<"\n\nThe Subtraction of Your Values Is "<<sum;
break;
case '3':
puts("\n\nMultiplication Panel");
puts("Enter 1st Value");
cin>>i;
puts("\nEnter 2nd Value");
cin>>j;
sum=i*j;
cout<<"\n\nThe  Multiplication of Your Values Is "<<sum;
break;
case '4':
puts("\n\nDivision Panel");
puts("Enter 1st Value");
cin>>i;
puts("\nEnter 2nd Value");
cin>>j;
div=i/j;
cout<<"\n\nThe Division of Your Values Is "<<div;
break;
default:
cout<<"Invalid Choice";
break;
}
getch();
}