Practice 50 Swift Conditional Statements Questions
Q1. Write a Swift program to check if a number is positive, negative, or zero.
Input:
let num = 5
Expected Output:
Positive number
Q2. Write a Swift program to find the largest of three numbers.
Input:
let a = 10, b = 20, c = 15
Expected Output:
Largest number is 20
Q3. Write a Swift program to check if a number is even or odd.
Input:
let num = 7
Expected Output:
Odd number
Q4. Write a Swift program to check if a year is a leap year.
Input:
let year = 2020
Expected Output:
Leap year
Q5. Write a Swift program to check if a given number is divisible by both 3 and 5.
Input:
let num = 15
Expected Output:
Divisible by both 3 and 5
Q6. Write a Swift program to check if a number is divisible by 7 but not by 5.
Input:
let num = 14
Expected Output:
Divisible by 7 but not by 5
Q7. Write a Swift program to check if a number is within a specified range (inclusive).
Input:
let num = 5, range = 1...10
Expected Output:
Number is in range
Q8. Write a Swift program to check if a character is a vowel or a consonant.
Input:
let char: Character = "A"
Expected Output:
Vowel
Q9. Write a Swift program to check if a string is empty.
Input:
let str = "Swift"
Expected Output:
String is not empty
Q10. Write a Swift program to find if a number is prime.
Input:
let num = 7
Expected Output:
Prime number
Q11. Write a Swift program to check if a string contains a specific character.
Input:
let str = "Hello", char: Character = "e"
Expected Output:
Contains the character e
Q12. Write a Swift program to find the minimum of three numbers.
Input:
let a = 5, b = 10, c = 3
Expected Output:
Minimum number is 3
Q13. Write a Swift program to check if a number is greater than 100.
Input:
let num = 150
Expected Output:
Number is greater than 100
Q14. Write a Swift program to check if a string starts with a particular character.
Input:
let str = "Swift", char: Character = "S"
Expected Output:
Starts with S
Q15. Write a Swift program to check if a number is within the range of 1 to 100.
Input:
let num = 45
Expected Output:
Number is in range 1 to 100
Q16. Write a Swift program to check if a number is positive or negative or zero using switch statement.
Input:
let num = -3
Expected Output:
Negative number
Q17. Write a Swift program to compare two strings and print if they are equal or not.
Input:
let str1 = "Swift", str2 = "swift"
Expected Output:
Strings are not equal
Q18. Write a Swift program to find if a number is divisible by 3, 5, or both using a switch statement.
Input:
let num = 15
Expected Output:
Divisible by both 3 and 5
Q19. Write a Swift program to check if a string contains a number.
Input:
let str = "Swift123"
Expected Output:
String contains a number
Q20. Write a Swift program to check if a number is an Armstrong number.
Input:
let num = 153
Expected Output:
Armstrong number
Q21. Write a Swift program to check if a number is a perfect square.
Input:
let num = 16
Expected Output:
Perfect square
Q22. Write a Swift program to check if a number is even or odd using a ternary operator.
Input:
let num = 6
Expected Output:
Even number
Q23. Write a Swift program to check if a number is divisible by 4 but not by 100.
Input:
let num = 400
Expected Output:
Not divisible by 4 but divisible by 100
Q24. Write a Swift program to check if a given number is positive, negative or zero.
Input:
let num = 0
Expected Output:
Zero
Q25. Write a Swift program to determine if a number is odd or even and divisible by 3.
Input:
let num = 9
Expected Output:
Odd and divisible by 3
Q26. Write a Swift program to check if a number is greater than or equal to 100.
Input:
let num = 101
Expected Output:
Greater than or equal to 100
Q27. Write a Swift program to determine if a number is divisible by 3, 5, or both using a switch statement.
Input:
let num = 15
Expected Output:
Divisible by both 3 and 5
Q28. Write a Swift program to check if a string contains only uppercase letters.
Input:
let str = "HELLO"
Expected Output:
String contains only uppercase letters
Q29. Write a Swift program to check if a number is negative using a switch statement.
Input:
let num = -5
Expected Output:
Negative number
Q30. Write a Swift program to check if a number is a palindrome.
Input:
let num = 121
Expected Output:
Palindrome number
Q31. Write a Swift program to check if a character is a digit or not.
Input:
let char: Character = "3"
Expected Output:
Digit
Q32. Write a Swift program to check if a number is greater than 10 but less than 50.
Input:
let num = 25
Expected Output:
Between 10 and 50
Q33. Write a Swift program to check if a string is a palindrome.
Input:
let str = "madam"
Expected Output:
Palindrome string
Q34. Write a Swift program to check if a number is divisible by 3 or 5 using a switch statement.
Input:
let num = 10
Expected Output:
Divisible by 5
Q35. Write a Swift program to find the largest of two numbers using the ternary operator.
Input:
let a = 20, b = 30
Expected Output:
30
Q36. Write a Swift program to check if a number is a multiple of 3 and 5.
Input:
let num = 15
Expected Output:
Multiple of 3 and 5
Q37. Write a Swift program to find the largest of three numbers using a switch statement.
Input:
let a = 10, b = 20, c = 15
Expected Output:
Largest number is 20
Q38. Write a Swift program to find if a string contains spaces.
Input:
let str = "Hello World"
Expected Output:
Contains spaces
Q39. Write a Swift program to check if a string has more than 5 characters.
Input:
let str = "Hello"
Expected Output:
String has less than 5 characters
Q40. Write a Swift program to check if a string contains any special characters.
Input:
let str = "Hello@World"
Expected Output:
Contains special characters
Q41. Write a Swift program to check if a number is less than 10 using a switch statement.
Input:
let num = 5
Expected Output:
Less than 10
Q42. Write a Swift program to check if a number is divisible by 6.
Input:
let num = 18
Expected Output:
Divisible by 6
Q43. Write a Swift program to find the range of a number using conditional statements.
Input:
let num = 15
Expected Output:
Number is between 10 and 20
Q44. Write a Swift program to check if a string contains only alphabets.
Input:
let str = "Swift"
Expected Output:
String contains only alphabets
Q45. Write a Swift program to find the grade based on a score using if-else statements.
Input:
let score = 85
Expected Output:
Grade: B
Q46. Write a Swift program to check if a number is a multiple of 10.
Input:
let num = 50
Expected Output:
Multiple of 10
Q47. Write a Swift program to find the median of three numbers.
Input:
let a = 3, b = 7, c = 5
Expected Output:
Median is 5
Q48. Write a Swift program to check if a string is a substring of another string.
Input:
let str1 = "Hello", str2 = "Hel"
Expected Output:
Substring found
Q49. Write a Swift program to check if a number is divisible by 2, 3, or both.
Input:
let num = 6
Expected Output:
Divisible by both 2 and 3
Q50. Write a Swift program to print the days of the week using a switch statement.
Input:
let day = 4
Expected Output:
Thursday
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!