This program will
Add n number of terms according to your wish,
Subtract n number of terms according to your wish,
Multiply n number of terms according to your wish,
Divide two terms,
Determine factorial of an integer and
Determine power of an integer
Add n number of terms according to your wish,
Subtract n number of terms according to your wish,
Multiply n number of terms according to your wish,
Divide two terms,
Determine factorial of an integer and
Determine power of an integer
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class calc
{
float a,b,r,t;
int i,x,y;
long int f, p;
long double f1;
public:
void add();
void subtract();
void multiply();
void divide();
void factorial();
void power();
}cal;
void calc::add()
{
cout<<"How many terms you need to add?\n";
cin>>t;
r=0;
for(i=0;i<t;i++)
{
cout<<"Enter term "<<i+1<<":\t";
cin>>a;
r=r+a;
}
cout<<"Sum = "<<r<<endl;
}
void calc::subtract()
{
cout<<"Enter the largest term:\t";
cin>>b;
cout<<"How many terms you need to subtract?\n";
cin>>t;
r=b;
for(i=0;i<t;i++)
{
cout<<"Enter term "<<i+1<<":\t";
cin>>a;
r=r-a;
}
cout<<"Result = "<<r<<endl;
}
void calc::multiply()
{
cout<<"How many terms you need to multiply?\n";
cin>>t;
r=1;
for(i=0;i<t;i++)
{
cout<<"Enter term "<<i+1<<":\t";
cin>>a;
r=r*a;
}
cout<<"Result = "<<r<<endl;
}
void calc::divide()
{
cout<<"If you need to divide a with b then\n";
cout<<"Enter the value of a:\t";
cin>>a;
cout<<"Enter the value of b:\t";
cin>>b;
r=a/b;
cout<<"Result = "<<r<<endl;
}
void calc::factorial()
{
cout<<"Enter the value:\t";
cin>>x;
if(x<12)
{
f=1;
for(i=1;i<=x;i++)
{
f=f*i;
}
cout<<"Factorial = "<<f<<endl;
}
else
{
f1=1;
for(i=1;i<=x;i++)
{
f1=f1*i;
}
cout<<"Factorial = "<<f1<<endl;
}
}
void calc::power()
{
cout<<"Enter the base value:\t";
cin>>x;
cout<<"Enter the power value:\t";
cin>>y;
p=1;
for(i=1;i<=y;i++)
{
p=p*x;
}
cout<<endl<<x<<" to the power "<<y<<" is "<<p;
cout<<endl;
}
int main()
{
int choice;
while(1)
{
cout<<"Enter your choice\n";
cout<<"1. Addition\n2. Subtraction\n";
cout<<"3. Multiplication\n4. Division\n";
cout<<"5. Factorial\n6. Power\n7. Exit\n";
cin>>choice;
switch(choice)
{
case 1: cal.add();break;
case 2: cal.subtract();break;
case 3: cal.multiply();break;
case 4: cal.divide();break;
case 5: cal.factorial();break;
case 6: cal.power();break;
case 7: exit(0);
default: cout<<"Invalid Input\nTry Again!!\n";
}
}
}
Any confusion regarding this code then feel free to ask by commenting here... Share if you liked this Post using your favorite sharing service:
Subscribe Updates, Its FREE!