Calculate square of a number without using * , / and pow ( ) | PHP implementation to calculate square without using * and pow ( ) ; handle negative input ; Initialize result ; Add n to res n - 1 times ; Driver Code
< ? php function square ( $ n ) { if ( $ n < 0 ) $ n = - $ n ; $ res = $ n ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ res += $ n ; return $ res ; } for ( $ n = 1 ; $ n <= 5 ; $ n ++ ) echo " n = " , ▁ $ n , ▁ " , " , ▁ " n ^ 2 = " , square ( $ n ) , " STRNEWLINE ▁ " ; ? >