Input: 4 Output:4 Is Power of 2
Input: 4 Output:4 Is Power of 2
Input: 4 Output:4 Is Power of 2
Input : 4
Output :4 is power of 2
Input : 9
Output : 9 is power of 3
Input : 4
Output : 4 is power of 4
Input : 16, 2
Output : 16 is power of 2
Input : 1,2,3,6,7,8
Output : Array
[3] => 4
[4] => 5
6. Write a PHP program to find three numbers from an array such that the sum of
three consecutive numbers equal to zero.
Input : (-1,0,1,2,-1,-4)
Output : Array
[0] => -1 + 0 + 1 = 0
7. Write a PHP program to find three numbers from an array such that the sum of
three consecutive numbers equal to a given number.
Output : Array
(
[0] => 2 + 7 + 7 = 16
[1] => 7 + 1 + 8 = 16
8. Write a PHP program to compute and return the square root of a given
number.
Input : 16
Output : 4
9. Write a PHP program to find a single number in an array that doesn't occur
twice.
Input : array(5, 3, 4, 3, 4)
Output : Array
[0] => 5
[1] => 3
[2] => 4
[3] => 3
[4] => 4
)
Single Number: 5
10. Write a PHP program to find the single element in an array where every
element appears three times except for one.
Input : array(5, 3, 4, 3, 5, 5, 3)
Output : Array
[0] => 5
[1] => 3
[2] => 4
[3] => 3
[4] => 5
[5] => 5
[6] => 3
Single Number: 4
11. Write a PHP program to find the single element appears once in an array
where every element appears twice except for one.
Input : array(5, 3, 0, 3, 0, 5, 7, 7, 9)
Output : 9
12. Write a PHP program to add the digits of a positive integer repeatedly until
the result has a single digit.
Input : 48
Output : 3
Step 1: 5 + 9 = 14
Step 1: 1 + 4 = 5
Sample :
14. Write a PHP program to reverse the bits of an integer (32 bits unsigned).
Input : 1234
Output: 1260388352
For example, the sequence 5, 7, 9, 11, 13, 15 ... is an arithmetic progression with
common difference of 2.
17. Write a PHP program to compute the sum of the two reversed numbers and
display the sum in reversed form.
Input : 13, 14
Output : 72
Note : The result will not be unique for every number for example 31 is a
reversed form of several numbers of 13, 130, 1300 etc. Therefore all the leading
zeros will be omitted.
18. Write a PHP program where you take any positive integer n, if n is even,
divide it by 2 to get n / 2. If n is odd, multiply it by 3 and add 1 to obtain 3n + 1.
Repeat the process until you reach 1.
Input : 12
Output : Array
[0] => 12
[1] => 6
[2] => 3
[3] => 10
[4] => 5
[5] => 16
[6] => 8
[7] => 4
[8] => 2
[9] => 1
Example :
For instance, starting with n = 12, one gets the sequence 12, 6, 3, 10, 5, 16, 8, 4,
2, 1.
n = 19, for example, takes longer to reach 1: 19, 58, 29, 88, 44, 22, 11, 34, 17,
52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1.
19. Write a PHP program to check whether a given number is an ugly number.
Input : 12
Ugly numbers are positive numbers whose only prime factors are 2, 3 or 5. The
sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...
20. Write a PHP program to get the Hamming numbers upto a given numbers
also check whether a given number is a Hamming number.
Input : 1
Input : ('anagram','nagaram')
According to Wikipedia, an anagram is direct word switch or word play, the result
of rearranging the letters of a word or phrase to produce a new word or phrase,
using all the original letters exactly once; for example, the word anagram can be
rearranged into nag-a-ram.
22. Write a PHP program to push all zeros to the end of a array.
Input : array(0,2,3,4,6,7,10)
Output : Array
[0] => 2
[1] => 3
[2] => 4
[3] => 6
[4] => 7
[5] => 10
[6] => 0
Input : array(1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 6)
Output : 5
Note: The majority element is the element that appears more than n/2 times
where n is the number of elements in the array.
24. Write a PHP program to find the length of the last word in a string.
Output : 9
25. Write a PHP program to find the single number which occurs odd number of
times and other numbers occur even number of times.
Input : 4, 5, 4, 5, 2, 2, 3, 3, 2, 4, 4
Output : 2