
Solve 50 PHP While Loop Programming Questions
Q1. Write a PHP script to print numbers from 1 to 5 using a while loop.
Input: None
Expected Output:
1
2
3
4
5
Q2. Write a PHP script to calculate the sum of numbers from 1 to 10 using a while loop.
Input: None
Expected Output: Sum: 55
Q3. Write a PHP script to print all even numbers between 1 and 10 using a while loop.
Input: None
Expected Output:
2
4
6
8
10
Q4. Write a PHP script to display the multiplication table of 3 using a while loop.
Input: None
Expected Output:
3 x 1 = 3
3 x 2 = 6
...
3 x 10 = 30
Q5. Write a PHP script to count down from 5 to 1 using a while loop.
Input: None
Expected Output:
5
4
3
2
1
Q6. Write a PHP script to print all odd numbers between 1 and 10 using a while loop.
Input: None
Expected Output:
1
3
5
7
9
Q7. Write a PHP script to calculate the factorial of a given number using a while loop.
Input: 5
Expected Output: Factorial: 120
Q8. Write a PHP script to print the squares of numbers from 1 to 5 using a while loop.
Input: None
Expected Output:
1
4
9
16
25
Q9. Write a PHP script to reverse the digits of a given number using a while loop.
Input: 12345
Expected Output: Reversed: 54321
Q10. Write a PHP script to print the first 5 Fibonacci numbers using a while loop.
Input: None
Expected Output:
0
1
1
2
3
Q11. Write a PHP script to find the sum of digits of a given number using a while loop.
Input: 123
Expected Output: Sum of digits: 6
Q12. Write a PHP script to check if a given number is prime using a while loop.
Input: 7
Expected Output: 7 is a prime number.
Q13. Write a PHP script to find the greatest common divisor (GCD) of two numbers using a while loop.
Input: 48, 18
Expected Output: GCD: 6
Q14. Write a PHP script to generate the first 10 multiples of a number using a while loop.
Input: 4
Expected Output:
4
8
12
...
40
Q15. Write a PHP script to check if a number is a palindrome using a while loop.
Input: 121
Expected Output: 121 is a palindrome.
Q16. Write a PHP script to print the characters of a string one by one using a while loop.
Input: PHP
Expected Output:
P
H
P
Q17. Write a PHP script to find the smallest number in an array using a while loop.
Input: [3, 1, 4, 1, 5]
Expected Output: Smallest number: 1
Q18. Write a PHP script to print numbers from 1 to n using a while loop.
Input: n = 7
Expected Output:
1
2
...
7
Q19. Write a PHP script to find the product of numbers in an array using a while loop.
Input: [1, 2, 3, 4]
Expected Output: Product: 24
Q20. Write a PHP script to print a pattern using a while loop.
Input: 3
Expected Output:
*
**
***
Q21. Write a PHP script to find the average of numbers in an array using a while loop.
Input: [2, 4, 6, 8]
Expected Output: Average: 5
Q22. Write a PHP script to display numbers in reverse order from an array using a while loop.
Input: [5, 4, 3, 2, 1]
Expected Output:
1
2
3
4
5
Q23. Write a PHP script to print the sum of even numbers between 1 and 20 using a while loop.
Input: None
Expected Output: Sum: 110
Q24. Write a PHP script to check if a number is an Armstrong number using a while loop.
Input: 153
Expected Output: 153 is an Armstrong number.
Q25. Write a PHP script to count the number of vowels in a string using a while loop.
Input: Hello World
Expected Output: Number of vowels: 3
Q26. Write a PHP script to print all the factors of a given number using a while loop.
Input: 12
Expected Output:
1
2
3
4
6
12
Q27. Write a PHP script to find the length of a string using a while loop.
Input: Programming
Expected Output: Length: 11
Q28. Write a PHP script to convert a decimal number to binary using a while loop.
Input: 10
Expected Output: Binary: 1010
Q29. Write a PHP script to print the ASCII values of characters in a string using a while loop.
Input: PHP
Expected Output:
P: 80
H: 72
P: 80
Q30. Write a PHP script to count the number of digits in a given number using a while loop.
Input: 12345
Expected Output: Number of digits: 5
Q31. Write a PHP script to print the first n multiples of 5 using a while loop.
Input: n = 5
Expected Output:
5
10
15
20
25
Q32. Write a PHP script to find the sum of the first n odd numbers using a while loop.
Input: n = 4
Expected Output: Sum: 16
Q33. Write a PHP script to calculate the power of a number using a while loop.
Input: Base = 2, Exponent = 3
Expected Output: Result: 8
Q34. Write a PHP script to remove duplicates from an array using a while loop.
Input: [1, 2, 2, 3, 3, 3]
Expected Output: [1, 2, 3]
Q35. Write a PHP script to reverse a string using a while loop.
Input: hello
Expected Output: Reversed: olleh
Q36. Write a PHP script to print the multiplication table of a given number up to n terms using a while loop.
Input: Number = 7, Terms = 5
Expected Output:
7 x 1 = 7
7 x 2 = 14
...
7 x 5 = 35
Q37. Write a PHP script to find the sum of the series 1 + 1/2 + 1/3 + ... + 1/n using a while loop.
Input: n = 3
Expected Output: Sum: 1.8333
Q38. Write a PHP script to display the elements of an array using a while loop.
Input: [10, 20, 30]
Expected Output:
10
20
30
Q39. Write a PHP script to count how many times a specific character appears in a string using a while loop.
Input: String = banana, Character = a
Expected Output: Occurrences: 3
Q40. Write a PHP script to generate the first n triangular numbers using a while loop.
Input: n = 4
Expected Output:
1
3
6
10
Q41. Write a PHP script to check if a number is a perfect number using a while loop.
Input: 28
Expected Output: 28 is a perfect number.
Q42. Write a PHP script to print the characters of a string in reverse order using a while loop.
Input: PHP
Expected Output:
P
H
P
Q43. Write a PHP script to calculate the product of digits of a given number using a while loop.
Input: 123
Expected Output: Product: 6
Q44. Write a PHP script to find the maximum element in an array using a while loop.
Input: [1, 2, 3, 4, 5]
Expected Output: Maximum: 5
Q45. Write a PHP script to find the average of numbers between 1 and n using a while loop.
Input: n = 5
Expected Output: Average: 3
Q46. Write a PHP script to print the numbers in an array in reverse order using a while loop.
Input: [10, 20, 30]
Expected Output:
30
20
10
Q47. Write a PHP script to calculate the sum of squares of the first n numbers using a while loop.
Input: n = 3
Expected Output: Sum: 14
Q48. Write a PHP script to print a right-angle triangle pattern of stars using a while loop.
Input: Rows = 4
Expected Output:
*
**
***
****
Q49. Write a PHP script to calculate the LCM of two numbers using a while loop.
Input: 6, 8
Expected Output: LCM: 24
Q50. Write a PHP script to print the sum of cubes of the first n numbers using a while loop.
Input: n = 3
Expected Output: Sum: 36