all Swift code
slip qu.1
Write a Swift program to accept two integer values and return true if one is negative and one is positive. Return true only if both are negative
func test(x:Int,y:Int)->Bool{
if x>0 && y<0
{
return true
}
else if x<0 && y>0
{
return true
}
else if x<0 && y<0
{
return true
}
else{
return false
}
}
print(test(x:10,y:12))
print(test(x:-10,y:12))
print(test(x:10,y:-12))
print(test(x:-10,y:-12))
slip qu.2
Write a Swift program to test whether the last digit of the two given non-negative integer values are the same or not.
func same_last_Digit(_ a: Int, _ b: Int) -> Bool {
guard a < 0, b < 0
else
{
if a % 10 == b % 10
{
return true
}
else
{
return false
}
}
return false
}
print(same_last_Digit(3, 13))
print(same_last_Digit(24, 4))
print(same_last_Digit(12, 24))
silp qu.4
Write a Swift program that accept two integer values and to test which value is nearest to the value 10, or return 0 if both integers have same distance from 10
func close_10(_ a: Int, _ b: Int) -> Int {
if abs(10 - b) > abs(10 - a)
{
return a
}
else if abs(10 - b) < abs(10 - a)
{
return b
}
else
{
return 0
}
}
print(close_10(8, 13))
print(close_10(12, 7))
print(close_10(14, 6))
silp qu.5
Write a Swift program to check if a given non-negative number is a multiple of 3 or a multiple of 5.
func test35(num: Int) -> Bool {
if num % 3 == 0 || num % 5 == 0
{
return true
} else {
return false
}
}
print(test35(num: 33))
print(test35(num: 17))
print(test35(num: 15))
print(test35(num: 9))
silp qu.6
Write a Swift program to compute the sum of the two integers. If the values are equal return triple their sum.
func triple_sum(a: Int, b: Int) -> Int {
if a == b
{
return (a + b) * 3
}
else
{
return a + b
}
}
print(triple_sum(a: 1, b: 2))
print(triple_sum(a: 3, b: 2))
print(triple_sum(a: 2, b: 2))
silp qu.7
Write a Swift program to add "Is" to the front of a given string. However, if the string already begins with "Is", return the given string.
import Foundation
func isstring(word: String) -> String {
if word.hasPrefix("Is") == true
{
return word
}
else
{
return "Is (word)"
}
}
print(isstring(word: "Is swift"))
print(isstring(word: "xyz"))
silp qu.8
Write a Swift program to find the largest number among three given integers
func max_three(_ x: Int, _ y: Int, _ z: Int) -> Int {
if x > y, x > z
{
return x
}
else if y > z, y > x
{
return y
}
else if z > y, z > x
{
return z
}
else if x == y, y > z
{
return x
}
else if y == z, z > x
{
return y
}
else
{
return x
}
}
print(max_three(1, 2, 3))
print(max_three(3, 2, 1))
print(max_three(-3, -2, 0))
slip-9 one number is 20
func make_20(x:Int , y:Int)->Bool
{
if x==20 || y==20
{
return true
}
else
{
return false
}
}
print(make_20(x:20 , y:5 ))
print(make_20(x:10 , y:3))