Question:Program to print the Fibonacci Number

**Program:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# Program to display the Fibonacci sequence up to n-th term n= int(input("Enter Limit: ")) # first two terms n1 = 0 n2 = 1 count = 0 # check if the number of terms is valid if n <= 0: print "Fibonacci exist only for positive numbers" elif n == 1: print "Fibonacci sequence upto",n,":" print n1 else: print "Fibonacci sequence upto",n,":" while count < n: print n1, n3 = n1 + n2 # update values n1 = n2 n2 = n3 count += 1 |

**Explanation:**

A Fibonacci sequence is the integer sequence of 0, 1, 1, 2, 3, 5, 8….

The first two terms are 0 and 1. All other terms are obtained by adding the preceding two terms

Ex: 5^{th} term is sum of 4^{th} and 3^{rd} term;3=2+1

**Output:**

1.

1 2 3 4 5 |
Enter Limit: 10 Fibonacci sequence upto 10 : 0 1 1 2 3 5 8 13 21 34 |

1 2 3 |
Enter Limit: -5 Fibonacci exist only for positive numbers |

1 2 3 4 5 |
Enter Limit: 1 Fibonacci sequence upto 1 : 0 |

Do share your feedback in comments to help us adding more programs like this.

## Leave a Reply