Solve 50 PHP For Loop Programming Questions
Q1: Write a program to print numbers from 1 to 10 using a for loop.
Input: None
Expected Output:
1 2 3 4 5 6 7 8 9 10
Q2: Write a program to print the first 10 even numbers using a for loop.
Input: None
Expected Output:
2 4 6 8 10 12 14 16 18 20
Q3: Write a program to print the first 10 odd numbers using a for loop.
Input: None
Expected Output:
1 3 5 7 9 11 13 15 17 19
Q4: Write a program to print the multiplication table of 5 using a for loop.
Input: None
Expected Output:
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Q5: Write a program to print numbers from 10 to 1 in reverse order using a for loop.
Input: None
Expected Output:
10 9 8 7 6 5 4 3 2 1
Q6: Write a program to calculate the sum of numbers from 1 to 100 using a for loop.
Input: None
Expected Output:
Sum = 5050
Q7: Write a program to calculate the factorial of a number using a for loop.
Input: `5`
Expected Output:
Factorial = 120
Q8: Write a program to print all the characters of a string using a for loop.
Input: `Hello`
Expected Output:
H
e
l
l
o
Q9: Write a program to print the squares of numbers from 1 to 5 using a for loop.
Input: None
Expected Output:
1 4 9 16 25
Q10: Write a program to print the cubes of numbers from 1 to 5 using a for loop.
Input: None
Expected Output:
1 8 27 64 125
Q11: Write a program to find the sum of even numbers from 1 to 50 using a for loop.
Input: None
Expected Output:
Sum = 650
Q12: Write a program to find the sum of odd numbers from 1 to 50 using a for loop.
Input: None
Expected Output:
Sum = 625
Q13: Write a program to count the number of vowels in a string using a for loop.
Input: `Programming`
Expected Output:
Number of vowels = 3
Q14: Write a program to print all numbers divisible by 3 from 1 to 30 using a for loop.
Input: None
Expected Output:
3 6 9 12 15 18 21 24 27 30
Q15: Write a program to print all numbers from 1 to 50 that are not divisible by 5 using a for loop.
Input: None
Expected Output:
1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29 31 32 33 34 36 37 38 39 41 42 43 44 46 47 48 49
Q16: Write a program to calculate the product of numbers from 1 to 5 using a for loop.
Input: None
Expected Output:
Product = 120
Q17: Write a program to reverse a string using a for loop.
Input: `World`
Expected Output:
dlroW
Q18: Write a program to print the ASCII values of characters in a string using a for loop.
Input: `ABC`
Expected Output:
A: 65
B: 66
C: 67
Q19: Write a program to print the Fibonacci series up to 10 terms using a for loop.
Input: None
Expected Output:
0 1 1 2 3 5 8 13 21 34
Q20: Write a program to print a pattern of stars using a for loop (5 rows).
Input: None
Expected Output:
*
**
***
****
*****
Q21: Write a program to calculate the sum of digits of a number using a for loop.
Input: `12345`
Expected Output:
Sum = 15
Q22: Write a program to print numbers from 1 to 10 except 5 using a for loop.
Input: None
Expected Output:
1 2 3 4 6 7 8 9 10
Q23: Write a program to check if a number is prime using a for loop.
Input: `13`
Expected Output:
13 is a prime number.
Q24: Write a program to print the first N terms of the multiplication table of a given number.
Input: `Number = 7, N = 5`
Expected Output:
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
Q25: Write a program to calculate the average of numbers from 1 to 10 using a for loop.
Input: None
Expected Output:
Average = 5.5
Q26: Write a program to print all numbers from 1 to 100 that are divisible by both 3 and 5 using a for loop.
Input: None
Expected Output:
15 30 45 60 75 90
Q27: Write a program to find the largest number in an array using a for loop.
Input: `[3, 7, 2, 9, 5]`
Expected Output:
Largest number = 9
Q28: Write a program to find the smallest number in an array using a for loop.
Input: `[3, 7, 2, 9, 5]`
Expected Output:
Smallest number = 2
Q29: Write a program to count the occurrences of a specific number in an array using a for loop.
Input: Array: `[1, 2, 2, 3, 4, 2]`, Number: `2`
Expected Output:
Occurrences of 2 = 3
Q30: Write a program to check if a string is a palindrome using a for loop.
Input: `madam`
Expected Output:
madam is a palindrome.
Q31: Write a program to print the sum of elements in an array using a for loop.
Input: `[1, 2, 3, 4, 5]`
Expected Output:
Sum = 15
Q32: Write a program to print all the elements of an array in reverse order using a for loop.
Input: `[1, 2, 3, 4, 5]`
Expected Output:
5 4 3 2 1
Q33: Write a program to count the number of digits in a given number using a for loop.
Input: `12345`
Expected Output:
Number of digits = 5
Q34: Write a program to calculate the power of a number using a for loop.
Input: Base: `2`, Exponent: `3`
Expected Output:
Result = 8
Q35: Write a program to print all numbers from 1 to 50 that are divisible by 7 but not divisible by 3 using a for loop.
Input: None
Expected Output:
7 14 28 35 49
Q36: Write a program to print the GCD (Greatest Common Divisor) of two numbers using a for loop.
Input: `Number1 = 18, Number2 = 24`
Expected Output:
GCD = 6
Q37: Write a program to print all the prime numbers between 1 and 50 using a for loop.
Input: None
Expected Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Q38: Write a program to count how many elements in an array are greater than a given number using a for loop.
Input: Array: `[1, 3, 7, 9, 2]`, Number: `5`
Expected Output:
Count = 2
Q39: Write a program to print a number pyramid of 5 rows using a for loop.
Input: None
Expected Output:
1
22
333
4444
55555
Q40: Write a program to print the Fibonacci series up to a given number using a for loop.
Input: `15`
Expected Output:
0 1 1 2 3 5 8 13
Q41: Write a program to check if an array is sorted in ascending order using a for loop.
Input: `[1, 2, 3, 4, 5]`
Expected Output:
Array is sorted.
Q42: Write a program to calculate the average of elements in an array using a for loop.
Input: `[10, 20, 30, 40, 50]`
Expected Output:
Average = 30
Q43: Write a program to count the number of uppercase letters in a string using a for loop.
Input: `HeLLoWoRLd`
Expected Output:
Uppercase letters = 6
Q44: Write a program to count the number of lowercase letters in a string using a for loop.
Input: `HeLLoWoRLd`
Expected Output:
Lowercase letters = 4
Q45: Write a program to print the reverse of a number using a for loop.
Input: `12345`
Expected Output:
54321
Q46: Write a program to print all numbers from 1 to 100 that are divisible by either 4 or 6 using a for loop.
Input: None
Expected Output:
4 6 8 12 16 18 20 24 28 30 32 36 40 42 44 48 52 54 56 60 64 66 68 72 76 78 80 84 88 90 92 96 100
Q47: Write a program to calculate the sum of squares of numbers from 1 to 10 using a for loop.
Input: None
Expected Output:
Sum of squares = 385
Q48: Write a program to find the position of the largest number in an array using a for loop.
Input: `[10, 20, 5, 40, 30]`
Expected Output:
Position = 4
Q49: Write a program to print the factorial of all numbers from 1 to 5 using a for loop.
Input: None
Expected Output:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
Q50: Write a program to print a diamond pattern of stars using nested for loops (5 rows).
Input: None
Expected Output:
*
***
*****
*******
*********
*******
*****
***
*
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!