Simple Calculator In C++ Language
// Author : Niraj Rajendra Bhagvat
// Date : 08/08/2022
#include<iostream>
using namespace std;
int main()
{
float n1=0,n2=0,res=0;
/*Declaration of two numbers for
taking input */
char op='\0';
/*Declaration of character
as a operator*/
cout << "Enter first number ";
cin >> n1 ;
cout << "\nEnter the operation ";
cin >> op ;
cout << "\nEnter second number ";
cin >> n2 ;
/* Taking inputs of the first
& second number with the operator
to pass it in switch statement*/
switch(op)
{
case '+': // For adding two numbers
res = n1+n2;
cout << "\nAddition = "<<res;
break;
case '-': // For substracting two numbers
res = n1-n2;
cout << "\nSubstraction = "<<res;
break;
case '*': // For multiplication
case 'x': // If someone enters 'x' or 'X' for
case 'X': // multiplication it is also accepted
res = n1*n2;
cout << "\nMultiplication = "<<res;
break;
case '/': // For divide first number by second
res = n1/n2;
cout << "\nDivision = "<<res;
break;
default : /* If someone enters wrong operator
then this will be printed */
cout << "\nEnter valid operator";
}
return 0;
}
Comments