×

# How to solve (a+b)^2 in PHP programming ?

3
(518 Views)

Programming cannot be done without mathematics logic. I want to solve (a+b)2 in the PHP programming language. I am a beginner in PHP. I won't use the mathematics formula  (a+b)2 in the PHP  language. How can convert the mathematics formula (a+b)2 in PHP? It is a famous mathematical formula.

in PHP

(edited)
0

PHP programming is based on operators and operands. If you are already learned operators in the C language, you will understand easily. You can use mathematics formula (a+b) in the PHP language. First of all, you need to learn about PHP data types.

Think about the variables in mathematics formula (a+b)2 There is two variable in this formula a and b.

How to open the mathematics formula (a+b)2

``(a+b)2=a2+b2+2ab``

How can write the line mathematics line a2+b2+2ab

``a2+b2+2ab=a*a+b*b+2*a*b``

if a=2 and b=4

then,

``````a2+b2+2ab=2*2+4*4+2*2*4

= 4+16+16

=36``````

You can use this mathematics logic in PHP programming

Asterisk sign (*) is used for multiplication in computer programming languages.

We declare the variables in PHP like this –

``````\$a;

\$b;``````

Use the above mathematics login –

``a*a+b*b+2*a*b``

Let’s create a program for mathematics formula (a+b)2 in PHP language –

``````<?php
\$a=5;
\$b=6;
echo "(a+b)<sup>2</sup>=a<sup>2</sup>+b<sup>2</sup>+2ab";
\$result=(\$a*\$a+\$b*\$b+2*\$a*\$b);
echo "=".\$result;
?>``````

You will get the result as output –

``(a+b)2=a2+b2+2ab=36``

You can change variables (a,b) values according to your needs. Comment