Q:

Write a PHP program to check whether a given positive integer is a power of three

0

Write a PHP program to check whether a given positive integer is a power of three.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

<?php
function is_Power_of_three($n)
{
      $x = $n;
      while ($x % 3 == 0) {
      $x /= 3;
     }
       
	if($x == 1)
    {
		return "$n is power of 3";
    }
    else
    {
		return "$n is not power of 3";
    }
  
}
print_r(is_Power_of_three(9)."\n");
print_r(is_Power_of_three(81)."\n");
print_r(is_Power_of_three(21)."\n");
?>

Sample Output:

9 is power of 3                                             
81 is power of 3                                            
21 is not power of 3

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now