Horje
PHP Program to Find Square Root of a Number

Given a Number, the task is to find the Square Root of Number in PHP.

Examples:

Input: 15
Output: 5

Input: 30
Output: 5.477

There are three methods to find the square root of the given number, these are:

 

Using sqrt() Function

The sqrt() function is used to calculate the square root of a number.

Syntax:

float sqrt( $value )

Example:

PHP

<?php
  
function squareRoot($number) {
    return sqrt($number);
}
  
$num = 36;
echo "Square Root: " . squareRoot($num);
  
?>

Output

Square Root: 6

Using pow() Function

The pow() function is used to calculate the power of a number. Here, we calculate the power 1/2 or 0.5 of given number.

Syntax:

pow($number, 0.5);

Example:

PHP

<?php
  
function squareRoot($number) {
    return pow($number, 0.5);
}
  
$num = 30;
echo "Square Root: " . squareRoot($num);
  
?>

Output

Square Root: 5.4772255750517

Using Exponentiation Operator (**)

The exponential operator (**) is used to calculate the exponent power of number. Here, we use exponent power to 0.5.

Syntax:

$number ** 0.5;

Example:

PHP

<?php
  
function squareRoot($number) {
    return $number ** 0.5;
}
  
$num = 36;
echo "Square Root: " . squareRoot($num);
  
?>

Output

Square Root: 6



Reffered: https://www.geeksforgeeks.org


Geeks Premier League

Related
PHP Program to Merge two Sorted Arrays PHP Program to Merge two Sorted Arrays
PHP Program to Find LCM of Two Numbers PHP Program to Find LCM of Two Numbers
Hexadecimal to Decimal Conversion in PHP Hexadecimal to Decimal Conversion in PHP
PHP Program to Add Two Binary Numbers PHP Program to Add Two Binary Numbers
Binary to Decimal Conversion in PHP Binary to Decimal Conversion in PHP

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14