C++ program to make a simple calculator that can add, subtract, multiple, divide, power function and factorial (logic explained)
C++ program to make a simple calculator that can add, subtract, multiple, divide, power function and factorial.
Its the simple C++ program that can find power can calculate factorial can divide, multiply, add and subtract. The logic is very simply explained here.
Steps
User side:
1: User asked to enter an operator that he/she wants to operate
2: If user enters + , now he/she asked to enter two numbers to add.
3: User displayed addition
4: If user enters - , now he/she asked to enter two numbers to subtract.
5: User displayed subtraction
6: If user enters *, now he/she asked to enter two numbers to multiply.
7: User displayed multiplication.
8: If user enters / , now he/she asked to enter dividend and divisor to divide.
9: User displayed division.
10: If user enters % , now he/she asked to enter two numbers (this function works only with integers )
11: User displayed the modulus of number .
12: If user enters ^ , now he/she asked to enter a number and power of that number
13: User displayed the power of number .
14: If user enters ! , now he/she asked to enter a number to find its factorial.
15: User displayed the factorial of that number.
16: Now user has option to continue or exit the calculator .
Developer side
1: Declare variables that are going to be used in program.
2: Start a while until the variable 'entery' is not equal to 'Q' (explained further )
3: Input the operand from user in a char type variable 'opt'
4: Place a switch statement to the variable 'opt'
5: If user enters + , now he/she asked to enter two numbers to add. (its case one where opt=+)
6: User displayed addition
7: If user enters - , now he/she asked to enter two numbers to subtract.(its case two where opt=-)
8: User displayed subtraction
9: If user enters *, now he/she asked to enter two numbers to multiply.(its case three where opt=*)
10: User displayed multiplication.
11: If user enters / , now he/she asked to enter dividend and divisor to divide.(its case four where opt=/)
12: User displayed division.
13: If user enters % , now he/she asked to enter two numbers (its case five where opt=%)
14: User displayed the modulus of number .
15: If user enters ^ , now he/she asked to enter a number and power of that number (its case six where opt=^). Power is calculated using while loop explained below.
16: User displayed the power of number .
17: If user enters ! , now he/she asked to enter a number to find its factorial. (its case seven where opt=!). Factorial is calculated using while loop explained below.
18: User displayed the factorial of that number.
19: If user enters any other key display a message "wrong input"
20: Now user has option to continue or exit the calculator . This functionality is compulsory for a calculator. User asked to enter 'Q' at the end if he/she wants to calculator. To continue using it, user can enter any key.
21: Program ends here.
Code:
#include<iostream>
using namespace std;
int main()
{ //Variable declaration
float a,b;
int N,F=1;
int p=1;
char opt,entery='e';
//heading
cout<<"\t\"Simple calculator\""<<endl;
while(entery!='Q')
{
cout<<"Enter an operator that you want to operate from
(+,-,*, /, %,^,!)";
cin>>opt;
switch(opt)
{//addition
case '+':
cout<<"Enter first number";
cin>>a;
cout<<"Enter second number";
cin>>b;
cout<<"Addition of "<<a<<" and "<<b<<"is equal to ="<<a+b;
break;
case '-':
cout<<"Enter first number";
cin>>a;
cout<<"Enter second number";
cin>>b;
cout<<"Subtration of "<<a<<" and "<<b<<"is equal to ="<<a-b;
break;
case '*':
cout<<"Enter first number";
cin>>a;
cout<<"Enter second number";
cin>>b;
cout<<"Multiplication of "<<a<<" and "<<b<<"is equal to ="<<a*b;
break;
case '/':
cout<<"Enter first number";
cin>>a;
cout<<"Enter second number";
cin>>b;
cout<<"Division of "<<a<<" and "<<b<<"is equal to ="<<a/b<<endl;
break;
case '%':
int c,d;
cout<<"Mudolus is only possible of integer type variables
so enter only integers\n"<<endl;
cout<<"Enter first number";
cin>>c;
cout<<"Enter second number";
cin>>d;
cout<<"Mudolus of "<<c<<" and "<<d<<"is equal to ="<<c%d<<endl;
break;
case '^':
int e,f;
cout<<"Enter a number";
cin>>e;
cout<<"Enter power of that number";
cin>>f;
while(f!=0)
{
p=p*e;
f--;
}
cout<<"Result = "<<p<<endl;
break;
case '!':
cout<<"Enter a number whose factorial is to be find"<<endl;
cin>>N;
while(N>0)
{
F=F*N;
N=N-1;
}
cout<<"Factorial is = "<<F<<endl;
break;
default:
cout<<"Wrong input\n";
break;
}
cout<<"Press Q if you want to exist the calculator (note:
Q is CAPITAL) Press any other key to
again open the calculator"<<endl;
cin>>entery;
}
cout<<"Calculator has closed";
system("pause");
return 0;
}
Output:
Comments
Post a Comment