C++ Program to make a simple calculator having all basic functions (+,-,*, /, %,^,!)

Thursday, 22 December 2016

Statement : 
 C++ Program to make a simple calculator having all basic functions (+,-,*, /, %,^,!) 
Code : 
// C++ Program to make a simple calculator having all basic functions (+,-,*, /, %,^,!) 
#include<iostream>
using namespace std;
void main()
{
       cout << "\t\t\tCalculator\n\n";
       int a, b;
       char op;
       float res=1;
       do
       {
              cout << "Enter Operational operator (+,-,*, /, %,^,!) : ";
              cin >> op;
              if (op == '!')
              {
                     cout << "Enter Number Of Which You Want To Take Factorial : ";
                     cin >> a;
                     while (a > 0)
                     {
                           res = res*a;
                           a--;
                     }
              }
              else if (op == '-')
              {
                     cout << "Enter First Number : ";
                     cin >> a;
                     cout << "Enter Second Number : ";
                     cin >> b;
                     if (a > b)
                     {
                           res = a - b;
                     }
                     else
                     {
                           res = b - a;
                     }
              }
              else if (op == '/')
              {
                     cout << "Enter First Number : ";
                     cin >> a;
                     cout << "Enter Second Number : ";
                     cin >> b;
                     res = a / b;
              }
              else if (op == '*')
              {
                     cout << "Enter First Number : ";
                     cin >> a;
                     cout << "Enter Second Number : ";
                     cin >> b;
                     res = a * b;
              }
              else if (op == '+')
              {
                     cout << "Enter First Number : ";
                     cin >> a;
                     cout << "Enter Second Number : ";
                     cin >> b;
                     res = a + b;
              }
              else if (op == '%')
              {
                     cout << "Enter First Number : ";
                     cin >> a;
                     cout << "Enter Second Number : ";
                     cin >> b;
                     res = a % b;
              }
              else if (op == '^')
              {
                     cout << "Enter Number : ";
                     cin >> a;
                     cout << "Enter Power : ";
                     cin >> b;
                     res = pow(a, b);
              }
              else
              {
                     cout << "Please Rnter A Valid Operator.";
                     res = 0;
              }
              cout << "\n\n\nResult : " << res;
              cout << "\n\nPress Q For Quit The Program OR Press Any Key To Run Again.";
              cin >> op;
       } while (op != 'Q' ||op != 'q');
       cout << "\n\n";
       system("pause");

}

Mohammad Tayyab

Author & Editor

Imagination is more important than knowledge.

0 comments:

Post a Comment