# C++ Exercises

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

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

```
Example:

To calculate (a-b)2.
Formula: (a-b)2=a2+b2- 2*a*b.
Declare two variables they are integer data types a, b, and store
the result in z variable then declares an int data type.
To declare one variable int data type and one variable float data type,
then store result variable float data type.
Declare two variables float data type, then store variable float data type.
Declare a two-variable integer data types, then store variable int data type
or float data type.
Input a=2,b=3
Then result=(a - b)2=a2+b2- 2*a*b
(a-b)2 =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+b*b-2*a*b;
cout<<" (a-b)^2 of two numbers :"<<result<<endl;
return 0;
}

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

(a-b)^2 of two numbers :1

```
```
/* C++ Program to calculate (a-b)2 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,2)+pow(b,2)-2*a*b;
cout<<" (a-b)^2 of two numbers :"<<result<<endl;
return 0;
}

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

(a-b)^2 of two numbers :9

```