Skip to content

Commit 5baada1

Browse files
authored
Merge pull request #185 from Hansuuuuuuuuuu/master
Fibonacci in Swift
2 parents 483098c + ef50651 commit 5baada1

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

fibonacci/fibonacci.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
func fib(_ num: Int) {
2+
if num == 0 {
3+
print("Enter a digit greater than 0")
4+
}
5+
else if num == 1{
6+
print("Fibonacci: 0")
7+
}
8+
9+
else {
10+
var f = 0
11+
var f2 = 1
12+
var ftemp = 0
13+
print("Fibonacci: \(f), \(f2)", terminator:"")
14+
for _ in 1...num-2 {
15+
print(", \(f + f2)", terminator:"")
16+
ftemp = f + f2
17+
f = f2
18+
f2 = ftemp
19+
}
20+
}
21+
}
22+
23+
print("Enter number of Fibonacci terms: ")
24+
let input = Int(readLine()!)
25+
fib(input!)

0 commit comments

Comments
 (0)