Fibonacci Series

    0

    1

    Mahendra Kumar

    beginnerlogic

    Fibonacci series in Python | In the Fibonacci series, the next element will be the sum of the previous two elements. The Fibonacci sequence is a series of numbers where a number is found by adding up the two numbers before it. Starting with 0 and 1, the sequence goes 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on…

    As a rule, the expression is Xn= Xn-1+ Xn-2

    # take input
    num = int(input('Enter number of terms: '))
    # print fibonacci series
    a, b = 0, 1
    i = 0
    # check if the number of terms is valid
    if num <= 0:
        print('Please enter a positive integer.')
    elif num == 1:
        print('The Fibonacci series: ')
        print(a)
    else:
        print('The Fibonacci series: ')
        while i < num:
            print(a, end=' ')
            c = a + b
            a = b
            b = c
            i = i+1
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.