Tuesday, 6 December 2011

Calculator in C++

Q: Make a calculator which takes numbers as input then assigns operations at run time,
Show result according to operator assigned..





#include <iostream>
using namespace std;
int main()
{
double number1, number2, total;
int current_operation;
cout << "Please Enter First Number: "; 
cin >> number1; 
cout << "Please Enter Second Number: ";
cin >> number2;
cout << "Which operation would you like use?" << endl;
cout << "Type 1 for Plus." << endl;
cout << "Type 2 for Minus." << endl;
cout << "Type 3 for Divide." << endl;
cout << "Type 4 for Multiply." << endl;
cin >> current_operation;
if(current_operation == 1)
{
total = number1 + number2;
cout << "Your Answer is = " << total << endl;
}
else if(current_operation == 2)
{
total = number1 - number2;
cout << "Your Answer is = " << total << endl;
}
else if(current_operation == 3)
{
total = number1 / number2;
cout << "Your Answer is = " << total << endl;
}
else if(current_operation == 4)
{
total = number1 * number2;
cout << "Your Answer is = " << total << endl;
}
else
cout << "You Have Entered an Invalid Number."<<endl;
return 0;
}

No comments:

Post a Comment