Practice 50 Swift Loops Programming Questions
Q1. Write a Swift 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 Swift program to calculate the sum of numbers from 1 to 5 using a while loop.
Input: None
Expected Output: 15
Q3. Write a Swift program to print the first 5 even numbers using a for loop.
Input: None
Expected Output: 2 4 6 8 10
Q4. Write a Swift program to print the first 5 odd numbers using a for loop.
Input: None
Expected Output: 1 3 5 7 9
Q5. Write a Swift program to print the multiplication table of 3 up to 3 x 10 using a for loop.
Input: None
Expected Output: 3 6 9 12 15 18 21 24 27 30
Q6. Write a Swift program to find the factorial of a number (e.g., 5) using a for loop.
Input: 5
Expected Output: 120
Q7. Write a Swift program to calculate the sum of all elements in an array using a for loop.
Input: [2, 4, 6, 8]
Expected Output: 20
Q8. Write a Swift program to find the maximum value in an array using a for loop.
Input: [3, 1, 9, 4]
Expected Output: 9
Q9. Write a Swift program to reverse an array using a for loop.
Input: [1, 2, 3, 4]
Expected Output: [4, 3, 2, 1]
Q10. Write a Swift program to count the number of vowels in a string using a for loop.
Input: "hello"
Expected Output: 2
Q11. Write a Swift program to print the first 10 Fibonacci numbers using a while loop.
Input: None
Expected Output: 0 1 1 2 3 5 8 13 21 34
Q12. Write a Swift program to calculate the power of a number (e.g., 2^3) using a for loop.
Input: 2, 3
Expected Output: 8
Q13. Write a Swift program to print all the elements of a dictionary using a for loop.
Input: ["A": 1, "B": 2, "C": 3]
Expected Output: A: 1, B: 2, C: 3
Q14. Write a Swift program to print all numbers divisible by 3 from 1 to 20 using a for loop.
Input: None
Expected Output: 3 6 9 12 15 18
Q15. Write a Swift program to calculate the sum of digits of a number using a while loop.
Input: 123
Expected Output: 6
Q16. Write a Swift program to print a right-angled triangle pattern using a for loop.
Input: 3
Expected Output:
*
**
***
Q17. Write a Swift program to print numbers from 10 to 1 using a for loop.
Input: None
Expected Output: 10 9 8 7 6 5 4 3 2 1
Q18. Write a Swift program to print the squares of numbers from 1 to 5 using a for loop.
Input: None
Expected Output: 1 4 9 16 25
Q19. Write a Swift program to check if a number is prime using a for loop.
Input: 7
Expected Output: Prime
Q20. Write a Swift program to find the GCD of two numbers using nested loops.
Input: 12, 18
Expected Output: 6
Q21. Write a Swift program to calculate the sum of all odd numbers from 1 to 10 using a while loop.
Input: None
Expected Output: 25
Q22. Write a Swift program to print numbers from 1 to 10, skipping multiples of 3, using a for loop.
Input: None
Expected Output: 1 2 4 5 7 8 10
Q23. Write a Swift program to check if a number is a palindrome using a while loop.
Input: 121
Expected Output: Palindrome
Q24. Write a Swift program to count the number of characters in a string using a for loop.
Input: "Swift"
Expected Output: 5
Q25. Write a Swift program to print the ASCII values of characters from 'A' to 'E' using a for loop.
Input: None
Expected Output: 65 66 67 68 69
Q26. Write a Swift program to find the sum of even numbers in an array using a for loop.
Input: [1, 2, 3, 4, 5]
Expected Output: 6
Q27. Write a Swift program to count the occurrences of a character in a string using a for loop.
Input: "hello", "l"
Expected Output: 2
Q28. Write a Swift program to generate the first 5 multiples of 4 using a for loop.
Input: None
Expected Output: 4 8 12 16 20
Q29. Write a Swift program to print the reverse of a number using a while loop.
Input: 1234
Expected Output: 4321
Q30. Write a Swift program to print numbers from 1 to 50, but skip numbers divisible by 5 using a for loop.
Input: None
Expected Output: 1 2 3 4 6 7 8 9 11...
Q31. Write a Swift program to check if an array contains duplicate elements using a for loop.
Input: [1, 2, 3, 2]
Expected Output: Contains duplicates
Q32. Write a Swift program to count the number of elements greater than 5 in an array using a for loop.
Input: [3, 6, 9, 2]
Expected Output: 2
Q33. Write a Swift program to print the sum of diagonal elements of a 2D array using nested for loops.
Input: [[1, 2], [3, 4]]
Expected Output: 5
Q34. Write a Swift program to print all elements of a 2D array using nested for loops.
Input: [[1, 2], [3, 4]]
Expected Output: 1 2 3 4
Q35. Write a Swift program to print the elements of an array in reverse order using a for loop.
Input: [10, 20, 30]
Expected Output: 30 20 10
Q36. Write a Swift program to find the product of all elements in an array using a for loop.
Input: [1, 2, 3]
Expected Output: 6
Q37. Write a Swift program to print all numbers between 1 and 20 that are divisible by 4 using a for loop.
Input: None
Expected Output: 4 8 12 16 20
Q38. Write a Swift program to calculate the average of an array using a for loop.
Input: [2, 4, 6, 8]
Expected Output: 5
Q39. Write a Swift program to count the number of digits in a number using a while loop.
Input: 12345
Expected Output: 5
Q40. Write a Swift program to print the cubes of numbers from 1 to 5 using a for loop.
Input: None
Expected Output: 1 8 27 64 125
Q41. Write a Swift program to print numbers from 1 to 10 using a repeat-while loop.
Input: None
Expected Output: 1 2 3 4 5 6 7 8 9 10
Q42. Write a Swift program to find the sum of all numbers between 1 and 100 using a for loop.
Input: None
Expected Output: 5050
Q43. Write a Swift program to print all prime numbers between 1 and 20 using a for loop.
Input: None
Expected Output: 2 3 5 7 11 13 17 19
Q44. Write a Swift program to reverse a string using a for loop.
Input: "Swift"
Expected Output: "tfiwS"
Q45. Write a Swift program to print a square pattern of asterisks using a for loop.
Input: 3
Expected Output:
***
***
***
Q46. Write a Swift program to find the LCM of two numbers using nested loops.
Input: 4, 6
Expected Output: 12
Q47. Write a Swift program to find the sum of squares of numbers from 1 to 4 using a for loop.
Input: None
Expected Output: 30
Q48. Write a Swift program to find the sum of elements in an array using a while loop.
Input: [5, 10, 15]
Expected Output: 30
Q49. Write a Swift program to print numbers from 1 to 100, but break the loop if the number is 50.
Input: None
Expected Output: 1 2 3...50
Q50. Write a Swift program to print all multiples of 7 up to 50 using a for loop.
Input: None
Expected Output: 7 14 21 28 35 42 49
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!