Write a program to mimic a calculator. Input two real numbers and state whether the numbers are to be added, subtracted, multiplied or divided. Cater for the possibility of denominator being zero in the division two numbers.
#include<iostream.h>
#include<conio.h>
#include<iomanip>
void main()
{ clrscr();
int a, b;
char op;
cout << "Enter Two Numbers with Operand" << endl;
cin >> a >> op >> b;
if (op == '+')
{
cout << "Operation is to be performed is Addition" << endl;
cout << "\n\t" << a << "+" << b << "=" << a+b << endl;
}
else if (op == '-')
{
cout << "Operation is to be performed is Subtraction" << endl;
cout << "\n\t" << a << "-" << b << "=" << a-b << endl;
}
else if (op == '*')
{
cout << "Operation is to be performed is Multiplication" << endl;
cout << "\n\t" << a << "*" << b << "=" << a*b << endl;
}
else if (op == '/')
{
if (b == 0)
cout << "Division not possible " <<endl;
else
{
double g, h;
g = (double) a;
h = (double) b;
cout << "Operation is to be performed" << endl;
cout << fixed << setprecision(2) << g << "/" << h << "=" << g/h << endl;
}
}
else
{
cout << endl << "\t Invalid Operators or operand";
}
getche();
}
No comments:
Post a Comment