Monday, November 13, 2017

Fibonacci Sequence

An integer value N is passed as the input. The program must print the first N terms in the Fibonacci sequence.

Input Format:
The first line denotes the value of N.

Output Format:
The first N terms in the Fibonacci sequence (with each term separated by a space)

Boundary Conditions: 3 <= N <= 50

Example Input/Output 1:
Input:
5
Output:
0 1 1 2 3

Solution:
n,a=int(input()),[]
for i in range(n):
    a.append(i) if i<=1 else a.append(a[i-1]+a[i-2])
print(*a)
Share: