This post contains the c++ example code for performing arithmetic operations.
In this example, each arithmetic function is explained using separate functions.
Copy and paste the below code in a text file, and rename it are filename.cpp ...
/* program to perform arithmetic operations*/
#include<iostream.h>
#include<process.h>
#include<conio.h>
class Arith
{
private:
int x,y;
public:
int add();
int sub();
int mul();
int div();
};
int Arith::add()
{
cout<<"Enter the x value"<<endl;
cin>>x;
cout<<"Enter the y value"<<endl;
cin>>y;
return(x+y);
}
int Arith::sub()
{
cout<<"Enter the x value"<<endl;
cin>>x;
cout<<"Enter the y value"<<endl;
cin>>y;
return(x-y);
}
int Arith::mul()
{
cout<<"Enter the x value"<<endl;
cin>>x;
cout<<"Enter the y value"<<endl;
cin>>y;
return(x*y);
}
int Arith::div()
{
cout<<"Enter the x value"<<endl;
cin>>x;
cout<<"Enter the y value"<<endl;
cin>>y;
return(x/y);
}
main()
{
Arith obj;
char ch;
clrscr();
while(1)
{
cout<<"a:Addition"<<endl;
cout<<"s:Subtraction"<<endl;
cout<<"m:Multiply"<<endl;
cout<<"d:Division"<<endl;
cout<<"e:exit"<<endl;
cout<<"Enter your Choice"<<endl;
cin>>ch;
switch(ch)
{
case 'a':
int a=obj.add();
cout<<"Addtion Result="<<a<<endl;
break;
case 's':
int s=obj.sub();
cout<<"Subtract Result="<<s<<endl;
break;
case 'm':
int m=obj.mul();
cout<<"Multiply Result="<<m<<endl;
break;
case 'd':
int d=obj.div();
cout<<"Division Result="<<d<<endl;
break;
case 'e':
exit(0);
}
}
getch();
} Hope this helps..
Techytips


October 10, 2011
S A Delphin












