
Practice 150 PHP Basic Algorithm Programming Questions
Q1. Write a PHP program to find the factorial of a given number.
Input:
$n = 5;
Expected Output:
Factorial of 5: 120
Q2. Write a PHP program to find the sum of the digits of a number.
Input:
$n = 123;
Expected Output:
Sum of Digits: 6
Q3. Write a PHP program to check if a given number is prime.
Input:
$n = 7;
Expected Output:
7 is a Prime Number
Q4. Write a PHP program to print the Fibonacci sequence up to a given number of terms.
Input:
$terms = 5;
Expected Output:
Fibonacci Sequence: 0, 1, 1, 2, 3
Q5. Write a PHP program to reverse the digits of a number.
Input:
$n = 12345;
Expected Output:
Reversed Number: 54321
Q6. Write a PHP program to find the greatest common divisor (GCD) of two numbers.
Input:
$a = 12;
$b = 15;
Expected Output:
GCD of 12 and 15: 3
Q7. Write a PHP program to check if a given number is even or odd.
Input:
$n = 4;
Expected Output:
4 is an Even Number
Q8. Write a PHP program to find the largest element in an array.
Input:
$array = [1, 5, 3, 9, 2];
Expected Output:
Largest Element: 9
Q9. Write a PHP program to sort an array in ascending order.
Input:
$array = [4, 1, 7, 3, 9];
Expected Output:
Sorted Array: [1, 3, 4, 7, 9]
Q10. Write a PHP program to find the missing number in a sequence of numbers.
Input:
$array = [1, 2, 4, 5, 6];
Expected Output:
Missing Number: 3
Q11. Write a PHP program to calculate the power of a number (base^exponent).
Input:
$base = 2;
$exponent = 3;
Expected Output:
2 raised to the power of 3: 8
Q12. Write a PHP program to count the number of words in a string.
Input:
$string = "Hello world, welcome to PHP!";
Expected Output:
Number of Words: 5
Q13. Write a PHP program to generate a multiplication table for a given number.
Input:
$number = 3;
Expected Output:
Multiplication Table of 3:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Q14. Write a PHP program to check if a given number is an Armstrong number.
Input:
$n = 153;
Expected Output:
153 is an Armstrong Number
Q15. Write a PHP program to find the length of a string.
Input:
$string = "hello";
Expected Output:
Length of the string: 5
Q16. Write a PHP program to convert a decimal number to binary.
Input:
$decimal = 10;
Expected Output:
Binary of 10: 1010
Q17. Write a PHP program to check if a given number is a perfect number.
Input:
$n = 28;
Expected Output:
28 is a Perfect Number
Q18. Write a PHP program to remove duplicate values from an array.
Input:
$array = [1, 2, 2, 3, 4, 4, 5];
Expected Output:
Array without duplicates: [1, 2, 3, 4, 5]
Q19. Write a PHP program to find the average of the numbers in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Average: 30
Q20. Write a PHP program to find the second largest element in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Second Largest Element: 40
Q21. Write a PHP program to check if a string contains a specific substring.
Input:
$string = "hello world";
$substring = "world";
Expected Output:
The string contains 'world': Yes
Q22. Write a PHP program to find the smallest element in an array.
Input:
$array = [10, 20, 5, 30, 40];
Expected Output:
Smallest Element: 5
Q23. Write a PHP program to reverse a string.
Input:
$string = "hello";
Expected Output:
Reversed String: olleh
Q24. Write a PHP program to convert a binary number to decimal.
Input:
$binary = "1010";
Expected Output:
Decimal of 1010: 10
Q25. Write a PHP program to find the sum of the first N natural numbers.
Input:
$n = 5;
Expected Output:
Sum of first 5 natural numbers: 15
Q26. Write a PHP program to check if a given number is a perfect square.
Input:
$n = 16;
Expected Output:
16 is a Perfect Square
Q27. Write a PHP program to count the number of occurrences of a specific character in a string.
Input:
$string = "hello world";
$char = "o";
Expected Output:
'o' appears 2 times in the string.
Q28. Write a PHP program to find the product of all elements in an array.
Input:
$array = [1, 2, 3, 4];
Expected Output:
Product of Elements: 24
Q29. Write a PHP program to find the common elements in two arrays.
Input:
$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
Expected Output:
Common Elements: [3, 4, 5]
Q30. Write a PHP program to check if a given number is a palindrome.
Input:
$n = 121;
Expected Output:
121 is a Palindrome Number
Q31. Write a PHP program to find the sum of the first N odd numbers.
Input:
$n = 5;
Expected Output:
Sum of first 5 odd numbers: 25
Q32. Write a PHP program to convert Celsius to Fahrenheit.
Input:
$celsius = 25;
Expected Output:
25°C is equal to 77°F
Q33. Write a PHP program to convert Fahrenheit to Celsius.
Input:
$fahrenheit = 77;
Expected Output:
77°F is equal to 25°C
Q34. Write a PHP program to find the maximum of three numbers.
Input:
$a = 12;
$b = 25;
$c = 19;
Expected Output:
Maximum of 12, 25, 19: 25
Q35. Write a PHP program to check if a year is a leap year.
Input:
$year = 2024;
Expected Output:
2024 is a Leap Year
Q36. Write a PHP program to find the number of even and odd numbers in an array.
Input:
$array = [1, 2, 3, 4, 5, 6];
Expected Output:
Even Numbers: 3
Odd Numbers: 3
Q37. Write a PHP program to check if a string is a palindrome.
Input:
$string = "madam";
Expected Output:
'madam' is a Palindrome String
Q38. Write a PHP program to swap two numbers without using a third variable.
Input:
$a = 5;
$b = 10;
Expected Output:
Swapped values: a = 10, b = 5
Q39. Write a PHP program to calculate the sum of all the elements in an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Sum of Array: 15
Q40. Write a PHP program to find the average of numbers in an array.
Input:
$array = [5, 10, 15, 20, 25];
Expected Output:
Average: 15
Q41. Write a PHP program to check if a string contains only numbers.
Input:
$string = "12345";
Expected Output:
The string contains only numbers: Yes
Q42. Write a PHP program to find the frequency of a character in a string.
Input:
$string = "hello world";
$char = "o";
Expected Output:
Frequency of 'o': 2
Q43. Write a PHP program to find the sum of all positive numbers in an array.
Input:
$array = [-1, 2, 3, -4, 5];
Expected Output:
Sum of Positive Numbers: 10
Q44. Write a PHP program to count the number of digits in a number.
Input:
$n = 12345;
Expected Output:
Number of digits: 5
Q45. Write a PHP program to check if a string is in uppercase.
Input:
$string = "HELLO";
Expected Output:
The string is in uppercase: Yes
Q46. Write a PHP program to find the median of an array.
Input:
$array = [1, 3, 5, 7, 9];
Expected Output:
Median: 5
Q47. Write a PHP program to find the mode of an array (the most frequent element).
Input:
$array = [1, 2, 2, 3, 3, 3, 4];
Expected Output:
Mode: 3
Q48. Write a PHP program to convert a string to lowercase.
Input:
$string = "HELLO WORLD";
Expected Output:
Lowercase String: hello world
Q49. Write a PHP program to print a triangle pattern of stars.
Input:
$rows = 5;
Expected Output:
*
**
***
****
*****
Q50. Write a PHP program to convert a string into an array of characters.
Input:
$string = "hello";
Expected Output:
Array: ['h', 'e', 'l', 'l', 'o']
Q51. Write a PHP program to find the largest prime number less than a given number N.
Input:
$n = 20;
Expected Output:
Largest Prime Number Less Than 20: 19
Q52. Write a PHP program to calculate the factorial of a number.
Input:
$n = 5;
Expected Output:
Factorial of 5: 120
Q53. Write a PHP program to convert a number to Roman numerals.
Input:
$n = 9;
Expected Output:
Roman Numeral: IX
Q54. Write a PHP program to find the sum of digits of a number.
Input:
$n = 12345;
Expected Output:
Sum of digits: 15
Q55. Write a PHP program to reverse the words in a sentence.
Input:
$sentence = "Hello World PHP";
Expected Output:
Reversed Sentence: PHP World Hello
Q56. Write a PHP program to check if a given number is prime.
Input:
$n = 17;
Expected Output:
17 is a Prime Number
Q57. Write a PHP program to find the second smallest element in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Second Smallest Element: 20
Q58. Write a PHP program to check if a given number is even or odd.
Input:
$n = 7;
Expected Output:
7 is Odd
Q59. Write a PHP program to find the number of vowels in a string.
Input:
$string = "hello";
Expected Output:
Number of Vowels: 2
Q60. Write a PHP program to sort an array in ascending order.
Input:
$array = [5, 3, 8, 1, 2];
Expected Output:
Sorted Array: [1, 2, 3, 5, 8]
Q61. Write a PHP program to merge two arrays into a single array.
Input:
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
Expected Output:
Merged Array: [1, 2, 3, 4, 5, 6]
Q62. Write a PHP program to remove duplicate elements from an array.
Input:
$array = [1, 2, 3, 2, 1, 4, 5];
Expected Output:
Array without duplicates: [1, 2, 3, 4, 5]
Q63. Write a PHP program to count the number of words in a string.
Input:
$string = "Hello World PHP";
Expected Output:
Number of Words: 3
Q64. Write a PHP program to convert a string to title case (first letter of each word capitalized).
Input:
$string = "hello world php";
Expected Output:
Title Case: Hello World Php
Q65. Write a PHP program to find the largest element in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Largest Element: 50
Q66. Write a PHP program to find the difference between two arrays (elements in the first array but not in the second).
Input:
$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];
Expected Output:
Difference: [1, 2]
Q67. Write a PHP program to check if a number is divisible by another number.
Input:
$num = 10;
$divisor = 2;
Expected Output:
10 is divisible by 2
Q68. Write a PHP program to find the GCD of two numbers.
Input:
$a = 60;
$b = 48;
Expected Output:
GCD of 60 and 48: 12
Q69. Write a PHP program to find the LCM of two numbers.
Input:
$a = 12;
$b = 15;
Expected Output:
LCM of 12 and 15: 60
Q70. Write a PHP program to check if a string starts with a specific word.
Input:
$string = "Hello World";
$word = "Hello";
Expected Output:
The string starts with 'Hello': Yes
Q71. Write a PHP program to check if a string ends with a specific word.
Input:
$string = "Hello World";
$word = "World";
Expected Output:
The string ends with 'World': Yes
Q72. Write a PHP program to find the power of a number (x raised to the power y).
Input:
$x = 2;
$y = 3;
Expected Output:
2 raised to the power of 3: 8
Q73. Write a PHP program to find the average of numbers in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Average: 30
Q74. Write a PHP program to convert a string into an array of words.
Input:
$string = "Hello World PHP";
Expected Output:
Array: ['Hello', 'World', 'PHP']
Q75. Write a PHP program to check if a string contains a specific substring.
Input:
$string = "Hello World";
$substring = "World";
Expected Output:
The string contains 'World': Yes
Q76. Write a PHP program to remove a specific element from an array.
Input:
$array = [1, 2, 3, 4, 5];
$element = 3;
Expected Output:
Array after removal: [1, 2, 4, 5]
Q77. Write a PHP program to find the length of a string.
Input:
$string = "Hello World";
Expected Output:
Length of string: 11
Q78. Write a PHP program to check if a number is a perfect number.
Input:
$n = 28;
Expected Output:
28 is a Perfect Number
Q79. Write a PHP program to find the sum of all even numbers in an array.
Input:
$array = [1, 2, 3, 4, 5, 6];
Expected Output:
Sum of Even Numbers: 12
Q80. Write a PHP program to check if a number is an Armstrong number.
Input:
$n = 153;
Expected Output:
153 is an Armstrong Number
Q81. Write a PHP program to find the factorial of a number using recursion.
Input:
$n = 5;
Expected Output:
Factorial of 5: 120
Q82. Write a PHP program to reverse a string using recursion.
Input:
$string = "hello";
Expected Output:
Reversed String: olleh
Q83. Write a PHP program to generate the Fibonacci sequence up to the nth term.
Input:
$n = 6;
Expected Output:
Fibonacci Sequence: 0 1 1 2 3 5
Q84. Write a PHP program to check if a string is a palindrome.
Input:
$string = "madam";
Expected Output:
'madam' is a Palindrome
Q85. Write a PHP program to find the sum of elements in an array using recursion.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Sum of Array: 15
Q86. Write a PHP program to calculate the power of a number using recursion.
Input:
$x = 2;
$y = 3;
Expected Output:
2 raised to the power of 3: 8
Q87. Write a PHP program to merge two arrays without duplicates.
Input:
$array1 = [1, 2, 3];
$array2 = [2, 3, 4];
Expected Output:
Merged Array without Duplicates: [1, 2, 3, 4]
Q88. Write a PHP program to create a simple calculator that performs addition, subtraction, multiplication, and division.
Input:
$num1 = 10;
$num2 = 5;
$operation = "addition";
Expected Output:
Result: 15
Q89. Write a PHP program to find the smallest element in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Smallest Element: 10
Q90. Write a PHP program to count the number of occurrences of a word in a string.
Input:
$string = "hello hello world hello";
$word = "hello";
Expected Output:
Number of occurrences of 'hello': 3
Q91. Write a PHP program to check if a given number is a perfect square.
Input:
$n = 16;
Expected Output:
16 is a Perfect Square
Q92. Write a PHP program to remove all whitespaces from a string.
Input:
$string = " Hello World ";
Expected Output:
String without whitespaces: HelloWorld
Q93. Write a PHP program to find the mode of an array.
Input:
$array = [1, 2, 3, 3, 4, 4, 4, 5];
Expected Output:
Mode: 4
Q94. Write a PHP program to replace a word in a string.
Input:
$string = "Hello World";
$oldWord = "World";
$newWord = "PHP";
Expected Output:
Replaced String: Hello PHP
Q95. Write a PHP program to check if an array is sorted in ascending order.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
The array is sorted in ascending order.
Q96. Write a PHP program to convert a string to uppercase.
Input:
$string = "hello world";
Expected Output:
Uppercase String: HELLO WORLD
Q97. Write a PHP program to convert a string to lowercase.
Input:
$string = "HELLO WORLD";
Expected Output:
Lowercase String: hello world
Q98. Write a PHP program to find the intersection of two arrays.
Input:
$array1 = [1, 2, 3, 4, 5];
$array2 = [4, 5, 6, 7];
Expected Output:
Intersection: [4, 5]
Q99. Write a PHP program to create a simple countdown timer that counts down from a given number of seconds.
Input:
$seconds = 10;
Expected Output:
Countdown: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Q100. Write a PHP program to generate a random number between two given numbers.
Input:
$min = 1;
$max = 100;
Expected Output:
Random Number: 42 (this will vary each time)
Q101. Write a PHP program to swap two variables without using a third variable.
Input:
$a = 5;
$b = 10;
Expected Output:
After swapping: a = 10, b = 5
Q102. Write a PHP program to remove the first element from an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Array after removal: [2, 3, 4, 5]
Q103. Write a PHP program to check if a given year is a leap year.
Input:
$year = 2020;
Expected Output:
2020 is a Leap Year
Q104. Write a PHP program to reverse an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Reversed Array: [5, 4, 3, 2, 1]
Q105. Write a PHP program to count the number of even numbers in an array.
Input:
$array = [1, 2, 3, 4, 5, 6];
Expected Output:
Count of Even Numbers: 3
Q106. Write a PHP program to sort an array in ascending order.
Input:
$array = [5, 3, 8, 1, 2];
Expected Output:
Sorted Array: [1, 2, 3, 5, 8]
Q107. Write a PHP program to sort an array in descending order.
Input:
$array = [5, 3, 8, 1, 2];
Expected Output:
Sorted Array: [8, 5, 3, 2, 1]
Q108. Write a PHP program to find the common elements in two arrays.
Input:
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
Expected Output:
Common Elements: [3, 4]
Q109. Write a PHP program to calculate the sum of odd numbers in an array.
Input:
$array = [1, 2, 3, 4, 5, 6];
Expected Output:
Sum of Odd Numbers: 9
Q110. Write a PHP program to create an array of even numbers from 1 to 20.
Input:
$n = 20;
Expected Output:
Even Numbers: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Q111. Write a PHP program to find the maximum value in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Maximum Value: 50
Q112. Write a PHP program to find the minimum value in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Minimum Value: 10
Q113. Write a PHP program to create an associative array from two indexed arrays.
Input:
$keys = ["name", "age", "city"];
$values = ["John", 25, "New York"];
Expected Output:
Associative Array: ["name" => "John", "age" => 25, "city" => "New York"]
Q114. Write a PHP program to find the length of an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Length of Array: 5
Q115. Write a PHP program to convert an array to a string.
Input:
$array = ["apple", "banana", "cherry"];
Expected Output:
Array as String: apple, banana, cherry
Q116. Write a PHP program to merge multiple arrays into one.
Input:
$array1 = [1, 2, 3];
$array2 = [4, 5, 6];
$array3 = [7, 8, 9];
Expected Output:
Merged Array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Q117. Write a PHP program to find the greatest common divisor (GCD) of two numbers.
Input:
$a = 36;
$b = 60;
Expected Output:
GCD of 36 and 60: 12
Q118. Write a PHP program to check if a number is prime.
Input:
$n = 7;
Expected Output:
7 is a Prime Number
Q119. Write a PHP program to find the sum of digits of a given number.
Input:
$n = 123;
Expected Output:
Sum of digits: 6
Q120. Write a PHP program to generate a random string of a given length.
Input:
$length = 8;
Expected Output:
Random String: f2xH8Z7p (output will vary each time)
Q121. Write a PHP program to remove duplicate values from an array.
Input:
$array = [1, 2, 2, 3, 4, 4, 5];
Expected Output:
Array without Duplicates: [1, 2, 3, 4, 5]
Q122. Write a PHP program to split a string into an array using a delimiter.
Input:
$string = "apple,banana,cherry";
$delimiter = ",";
Expected Output:
Array: ["apple", "banana", "cherry"]
Q123. Write a PHP program to calculate the average of numbers in an array.
Input:
$array = [10, 20, 30, 40, 50];
Expected Output:
Average: 30
Q124. Write a PHP program to print the first N Fibonacci numbers.
Input:
$n = 6;
Expected Output:
Fibonacci Sequence: 0, 1, 1, 2, 3, 5
Q125. Write a PHP program to convert an array to JSON format.
Input:
$array = ["name" => "John", "age" => 25, "city" => "New York"];
Expected Output:
{"name":"John","age":25,"city":"New York"}
Q126. Write a PHP program to calculate the area of a circle given its radius.
Input:
$radius = 5;
Expected Output:
Area of Circle: 78.54
Q127. Write a PHP program to check if a string contains a given substring.
Input:
$string = "Hello World";
$substring = "World";
Expected Output:
'Hello World' contains 'World'
Q128. Write a PHP program to remove the last element from an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Array after removal: [1, 2, 3, 4]
Q129. Write a PHP program to convert a string into an array.
Input:
$string = "Hello World";
Expected Output:
Array: ["Hello", "World"]
Q130. Write a PHP program to create an array of odd numbers from 1 to 20.
Input:
$n = 20;
Expected Output:
Odd Numbers: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Q131. Write a PHP program to find the median of an array.
Input:
$array = [1, 3, 4, 2, 5];
Expected Output:
Median: 3
Q132. Write a PHP program to merge two arrays and remove duplicates.
Input:
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
Expected Output:
Merged Array without Duplicates: [1, 2, 3, 4, 5, 6]
Q133. Write a PHP program to reverse a string.
Input:
$string = "PHP";
Expected Output:
Reversed String: PHP
Q134. Write a PHP program to calculate the factorial of a number.
Input:
$n = 5;
Expected Output:
Factorial: 120
Q135. Write a PHP program to generate a random number within a given range.
Input:
$min = 10;
$max = 50;
Expected Output:
Random Number: 27 (output will vary each time)
Q136. Write a PHP program to calculate the power of a number.
Input:
$base = 2;
$exponent = 3;
Expected Output:
Power: 8
Q137. Write a PHP program to remove all null values from an array.
Input:
$array = [1, null, 3, null, 5];
Expected Output:
Array without Null Values: [1, 3, 5]
Q138. Write a PHP program to count the occurrences of a specific value in an array.
Input:
$array = [1, 2, 3, 1, 4, 1];
$value = 1;
Expected Output:
Occurrences of 1: 3
Q139. Write a PHP program to check if a given string is a palindrome.
Input:
$string = "madam";
Expected Output:
madam is a Palindrome
Q140. Write a PHP program to merge multiple arrays into one array.
Input:
$array1 = [1, 2];
$array2 = [3, 4];
$array3 = [5, 6];
Expected Output:
Merged Array: [1, 2, 3, 4, 5, 6]
Q141. Write a PHP program to check if a given number is odd or even.
Input:
$number = 7;
Expected Output:
7 is Odd
Q142. Write a PHP program to calculate the sum of all elements in an array.
Input:
$array = [1, 2, 3, 4, 5];
Expected Output:
Sum: 15
Q143. Write a PHP program to find the difference between two arrays.
Input:
$array1 = [1, 2, 3, 4];
$array2 = [3, 4, 5, 6];
Expected Output:
Difference: [1, 2]
Q144. Write a PHP program to convert a string to uppercase.
Input:
$string = "hello world";
Expected Output:
HELLO WORLD
Q145. Write a PHP program to check if a number is positive, negative, or zero.
Input:
$number = -5;
Expected Output:
-5 is Negative
Q146. Write a PHP program to remove leading and trailing white spaces from a string.
Input:
$string = " Hello World! ";
Expected Output:
Hello World!
Q147. Write a PHP program to calculate the length of a string.
Input:
$string = "PHP Programming";
Expected Output:
Length: 16
Q148. Write a PHP program to print the multiplication table of a given number.
Input:
$number = 3;
Expected Output:
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
...
Q149. Write a PHP program to find the first non-repeated character in a string.
Input:
$string = "swiss";
Expected Output:
First Non-Repeated Character: w
Q150. Write a PHP program to calculate the square root of a given number.
Input:
$number = 16;
Expected Output:
Square Root: 4