Fibonacci Series

Problem Statement

In this challenge, you should print the Fibonacci series with specified number of values.

The Fibonacci Series is

0 1 1 2 3 5 8 13 21 34 55 89 ...

Input

The first line of input consists of an integer n, where n>=0, which represents the number of values in the Fibonacci Series.

Output

Print first n values of the Fibonacci Series.

As is

The boilerplate code is already there to read the input from user, in the main() function, and to print the list returned by the fibonacciSeries() function. Do not edit this code.

To do ✍

fibonacciSeries() has one parameter: n. The function returns the values of Fibonacci Series as a list. In the main function, we already have code to join the elements of the returned list with a space and print it to output.

Example 1

Test input

10

Test output

0 1 1 2 3 5 8 13 21

Explanation

Only the first ten values of the Fibonacci Series should be printed to the output.

Example 2

Test input

1

Test output

0

Explanation

Only the first value of the Fibonacci Series should be printed to the output.

Python Program

# Complete the following function
def fibonacciSeries(n):








# Boilerplate code - Do not edit the following
def main():
    n = int(input())
    series = [ str(x) for x in fibonacciSeries(n)]
    print(' '.join(series))

if __name__ == "__main__":
    main()

Input and Outputs 1

5
0 1 1 2 3

Input and Outputs 2

20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Input and Outputs 3

1
0

Input and Outputs 4

0

Input and Outputs 5

2
0 1
# Complete the following function
def fibonacciSeries(n):
    series = [0,1]

    if n>2:
        for i in range(2, n):
            #next element in series = sum of its previous two numbers
            nextElement = series[i-1] + series[i-2]
            #append the element to the series
            series.append(nextElement)
        return series
    else:
        return series[0:n]

# Boilerplate code - Do not edit the following
def main():
    n = int(input())
    series = [ str(x) for x in fibonacciSeries(n)]
    print(' '.join(series))

if __name__ == "__main__":
    main()
show answer
main.py
⏵︎ Run Tests
Reset
Download
×
Code copied to clipboard successfully 👍