You are on page 1of 2

10. Write a program to find factorial or a number.

var number: Int = 5 // Change the number here

var fact: Int = 1

var n: Int = number + 1

for i in 1..<n{

fact = fact * i

}
print("Factorial of ",number," is: ", fact)

Output:
Factorial of 5 is: 120

11. Write a program to swap two numbers.


func swap(a: Int ,b: Int) -> (Int,Int){

return (b,a)
}
var a: Int = 10 , b: Int = 20

print("Before swapping", a, b)

var s = swap(a:a,b:b)

print("After swapping ", s.0, s.1 )

Output:
Before swapping 10 20
After swapping 20 10
12. Write a program to check palindrome number.
var reverse = 0
var rem = 0
var num = 141
var n = num

while num != 0 {
rem = num%10
reverse = reverse * 10 + rem
num = num/10
}
if(n == reverse)
{
print("\(n)" + " is a palindrome.")
}
else
{
print("\(n)" + " is not a palindrome.")

Output:
141 is a palindrome.

You might also like