# C++ Exercises

## C++ Program to calculate (a - b)3.

Let see the program to calculate (a - b)3.

```
Example:

To calculate (a - b)3.
Formula: (a - b)3 =a3 – 3*a2*b + 3*a*b2 – b3
Input a=1,b=2
(a - b)3 =-1

```
```
#include <iostream>

using namespace std;
int main()
{
int a,b,result;
cout<<"Enter a value :";
cin>>a;
cout<<"Enter b value :";
cin>>b;
result=a*a*a -b*b*b -3*a*a*b +3*a*b*b;
cout<<" Result (a - b)^3 = :"<<result<<endl;
return 0;
}

```
##### Output:
```
Enter a value :1
Enter b value :2

Result (a - b)^3 = :-1

```
```
#include <iostream>

using namespace std;
int main()
{
int a,b,result;
cout<<"Enter a value :";
cin>>a;
cout<<"Enter b value :";
cin>>b;
result=a*a*a -b*b*b -3*a*b*(a -b);
cout<<" Result (a - b)^3 = :"<<result<<endl;
return 0;
}

```
##### Output:
```
Enter a value :5
Enter b value :7

Result (a - b)^3 = :-8

```
```
/* C++ Program to calculate (a-b)3 using pow() function. */
#include <iostream>

using namespace std;
#include <math.h>
int main()
{
int a,b,result;
cout<<"Enter a value :";
cin>>a;
cout<<"Enter b value :";
cin>>b;
result=pow(a,3) - 3*pow(a,2)*b + 3*a*pow(b,2) - pow(b,3)  ;
cout<<" Result (a - b)^3 = :"<```
##### Output:
```
Enter a value :8
Enter b value :3

Result (a - b)^3 = :125

```
```
/* C++ Program to calculate (a-b)3 using pow() function. */
#include <iostream>

using namespace std;
#include <math.h>
int main()
{
int a,b,result;
cout<<"Enter a value :";
cin>>a;
cout<<"Enter b value :";
cin>>b;
result=pow(a,3) -pow(b,3) -3*a*b*(a -b);
cout<<" Result (a - b)^3 = :"<<result<<endl;
return 0;
}

```
##### Output:
```
Enter a value :10
Enter b value :4

Result (a - b)^3 = :216

```