The Fibonacci sequence is constructed by adding the last two numbers of the sequence so far to get the next number in the sequence. The first and the second numbers of the sequence are defined as 0 and 1. We get:
0, 1, 1, 2, 3, 5, 8, 13, 21…
Write a function which takes input as a number:
int getFibOutput(int input) {
// TODO:
}
Example
(21 is a Fibonacci number)
Input: 21 Output: 21
(20 is NOT a Fibonacci number so, output is 10 (13+8+5+3+2+1+1))
Input: 20 Output: 33
def getFibOutput(n):
a=0
b=1
c=a+b
Flag=0
Sum = 1
num=[]
num.append(a)
num.append(b)
while(c <= n):
c = a+b
if(c==n):
Flag=1
print("(",n,") is a Fibonacii Number.")
num.append(c)
a=b
b=c
if(Flag==0):
Sum=0
s = "Sum = "
for r in range(0,len(num)-1):
Sum = Sum + num[r]
s = s + str(num[r]) + "+"
print(n,"is not a Fibonacii number.")
print(s,"=",Sum)
getFibOutput(21)
Comments
Leave a comment