Write a PHP program to check whether an integer is the power of another integer

  • برمجة بي اتش بي

Write a PHP program to check whether an integer is the power of another integer.

Input : 16, 2

Example: For x = 16 and y = 2 the answer is "true", and for x = 12 and y = 2 "false"

الأجوبة

<?php
function is_Power($x, $y)
{
      $a = $x;
      $b = $y;
      while ($x % $y == 0) {
       $x = $x / $y;
     }
       
	if($x == 1)
    {
		return "$a is power of $b";
    }
    else
    {
		return "$a is not power of $b";
    }
  
}
print_r(is_Power(16,2)."\n");
print_r(is_Power(12,2)."\n");
print_r(is_Power(81,3)."\n");
?>

Sample Output:

16 is power of 2                                            
12 is not power of 2                                        
81 is power of 3
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...