Pages

Tuesday 8 December 2015

Implementation of $(a+b)^2$ and $(a-b)^2$ operation using C++


Algorithm:


The basic mathematical operation $(a+b)^2$ = ${a^2} + {b^2} + {2ab}$ and the operation $(a-b)^2$ = ${a^2} + {b^2} - {2ab}$

#include "iostream"

using namespace std;

class Mathematics
{
    int a,b;
public:
    void input()
    {
        cout<< "input a:\n";
        cin >> a;
        cout << "input b:\n";
        cin >> b;
    }

    void add()
    {
        cout << "(a+b)^2=" << ((a*a)+(2*a*b)+(b*b));
    }
    
    void sub()
    {
        cout << "(a-b)^2=" << ((a*a)-(2*a*b)+(b*b));
    }
};

int main()
{
    Mathematics m;

    m.input();
    m.add();
    m.sub();

    return (0);
}

No comments:

Post a Comment