Monday, October 10, 2011

C++ example program to perform arithmetic operations like addition, subtraction, multiplication and division.



   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

6 comments:

Unknown said...

Fucking program can't understand single line

Malik aesthetic said...

Bsdk galat h

Unknown said...

Member function outside the class body. in public functions are declared then return type class name::member function

trustno1 said...

c programming examples
c example - calculate Area of a Polygon

Unknown said...

Vc

SP said...

after entering choice program execution was closed ☹️

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Bluehost Coupons