Write A Python Function To Get The Fibonacci Series Between 0 To 50

Fibonacci of a number is defined as addition of Fibonacci of (number - 2) and Fibonacci of (number - 1). Where these conditions apply Fibonacci of 0 is 0 and Fibonacci of one is one

Below is the Fibonacci function

fib(0) = 0
fib(1) = 1
fib(n) = fib(n-1) + fib(n-2)

 

Write A Python Function To Get The Fibonacci Series Between 0 To 50

 

 


Write A Python Function To Get The Fibonacci Series Between 0 To 50

Let's define a function fib50()  and initialize two integer variables a and b set to 0 and 1. Iterate from 0 to 51 using the for loop.

With in the for loop
==========================
1) Print the value of integer "a" using the print() function call.
2) Swap the values of a and b where a = b and b = a + b.


def fib50():
   a = 0
   b = 1
   for index in range(51):
       print(f"Fib of {index} is {a}")
       a, b = b, a + b
      




In the main program just print the fibonacci of 0 to 50  and call the function that is defined as above fib50().

print("Fibonacci Series from 0 to 50 is ... ")
fib50()




Final Program
==================

def fib50():
   a = 0
   b = 1
   for index in range(51):
       print(f"Fib of {index} is {a}")
       a, b = b, a + b


print("Fibonacci Series from 0 to 50 is ... ")
fib50()






OUTPUT
============

Fibonacci Series from 0 to 50 is ...
Fib of 0 is 0
Fib of 1 is 1
Fib of 2 is 1
Fib of 3 is 2
Fib of 4 is 3
Fib of 5 is 5
Fib of 6 is 8
Fib of 7 is 13
Fib of 8 is 21
Fib of 9 is 34
Fib of 10 is 55
Fib of 11 is 89
Fib of 12 is 144
Fib of 13 is 233
Fib of 14 is 377
Fib of 15 is 610
Fib of 16 is 987
Fib of 17 is 1597
Fib of 18 is 2584
Fib of 19 is 4181
Fib of 20 is 6765
Fib of 21 is 10946
Fib of 22 is 17711
Fib of 23 is 28657
Fib of 24 is 46368
Fib of 25 is 75025
Fib of 26 is 121393
Fib of 27 is 196418
Fib of 28 is 317811
Fib of 29 is 514229
Fib of 30 is 832040
Fib of 31 is 1346269
Fib of 32 is 2178309
Fib of 33 is 3524578
Fib of 34 is 5702887
Fib of 35 is 9227465
Fib of 36 is 14930352
Fib of 37 is 24157817
Fib of 38 is 39088169
Fib of 39 is 63245986
Fib of 40 is 102334155
Fib of 41 is 165580141
Fib of 42 is 267914296
Fib of 43 is 433494437
Fib of 44 is 701408733
Fib of 45 is 1134903170
Fib of 46 is 1836311903
Fib of 47 is 2971215073
Fib of 48 is 4807526976
Fib of 49 is 7778742049
Fib of 50 is 12586269025


Write A Python Function To Get The Fibonacci Series Between 0 To 100
 
def fib100():
   a = 0
   b = 1
   for index in range(101):
       print(f"Fib of {index} is {a}")
       a, b = b, a + b
      

print("Fibonacci Series from 0 to 100 is ... ")
fib100()


OUTPUT
======================
Fibonacci Series from 0 to 100 is ...
Fib of 0 is 0
Fib of 1 is 1
Fib of 2 is 1

Fib of 3 is 2
Fib of 4 is 3
Fib of 5 is 5
Fib of 6 is 8
Fib of 7 is 13
Fib of 8 is 21
Fib of 9 is 34
Fib of 10 is 55
Fib of 11 is 89
Fib of 12 is 144
Fib of 13 is 233
Fib of 14 is 377
Fib of 15 is 610
Fib of 16 is 987
Fib of 17 is 1597
Fib of 18 is 2584
Fib of 19 is 4181
Fib of 20 is 6765
Fib of 21 is 10946
Fib of 22 is 17711
Fib of 23 is 28657
Fib of 24 is 46368
Fib of 25 is 75025
Fib of 26 is 121393
Fib of 27 is 196418
Fib of 28 is 317811
Fib of 29 is 514229
Fib of 30 is 832040
Fib of 31 is 1346269
Fib of 32 is 2178309
Fib of 33 is 3524578
Fib of 34 is 5702887
Fib of 35 is 9227465
Fib of 36 is 14930352
Fib of 37 is 24157817
Fib of 38 is 39088169
Fib of 39 is 63245986
Fib of 40 is 102334155
Fib of 41 is 165580141
Fib of 42 is 267914296
Fib of 43 is 433494437
Fib of 44 is 701408733
Fib of 45 is 1134903170
Fib of 46 is 1836311903
Fib of 47 is 2971215073
Fib of 48 is 4807526976
Fib of 49 is 7778742049
Fib of 50 is 12586269025
Fib of 51 is 20365011074
Fib of 52 is 32951280099
Fib of 53 is 53316291173
Fib of 54 is 86267571272
Fib of 55 is 139583862445
Fib of 56 is 225851433717
Fib of 57 is 365435296162
Fib of 58 is 591286729879
Fib of 59 is 956722026041
Fib of 60 is 1548008755920
Fib of 61 is 2504730781961
Fib of 62 is 4052739537881
Fib of 63 is 6557470319842
Fib of 64 is 10610209857723
Fib of 65 is 17167680177565
Fib of 66 is 27777890035288
Fib of 67 is 44945570212853
Fib of 68 is 72723460248141
Fib of 69 is 117669030460994
Fib of 70 is 190392490709135
Fib of 71 is 308061521170129
Fib of 72 is 498454011879264
Fib of 73 is 806515533049393
Fib of 74 is 1304969544928657
Fib of 75 is 2111485077978050
Fib of 76 is 3416454622906707
Fib of 77 is 5527939700884757
Fib of 78 is 8944394323791464
Fib of 79 is 14472334024676221
Fib of 80 is 23416728348467685
Fib of 81 is 37889062373143906
Fib of 82 is 61305790721611591
Fib of 83 is 99194853094755497
Fib of 84 is 160500643816367088
Fib of 85 is 259695496911122585
Fib of 86 is 420196140727489673
Fib of 87 is 679891637638612258
Fib of 88 is 1100087778366101931
Fib of 89 is 1779979416004714189
Fib of 90 is 2880067194370816120
Fib of 91 is 4660046610375530309
Fib of 92 is 7540113804746346429
Fib of 93 is 12200160415121876738
Fib of 94 is 19740274219868223167
Fib of 95 is 31940434634990099905
Fib of 96 is 51680708854858323072
Fib of 97 is 83621143489848422977
Fib of 98 is 135301852344706746049
Fib of 99 is 218922995834555169026
Fib of 100 is 354224848179261915075

 

 

Conclusion:
===================

Change the for loop in the fib50() function change the value from 51 to 101 and see the difference by running the program.


Post a Comment

0 Comments