Answer to Question #287302 in Python for jayu

Question #287302

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:

  • If the given number is a Fibonacci number, print the number
  • If the given number is NOT a Fibonacci number, print the sum of all Fibonacci numbers less than the given 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




1
Expert's answer
2022-01-13T12:31:59-0500
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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS