Techiio-author
Started by Anik AdhikaryMay 14, 2022

Open
How to write python code , where the first two numbers are 1, and each number thereafter is the sum of the two preceding numbers?

4 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 4 VIEWES SHARE

Please write a program that asks the user how many Fibonacci numbers to print and then prints that many like 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 . . .

Please any solution to this, I tried my best but it doesn't work

1 Replies

Techiio-commentatorMotiar Rahaman replied a month ago0 likes0 dislikes

Here First two terms are 1 and 1. All other terms are obtained by adding the preceding two terms. This means to say the nth term is the sum of (n-1)th and (n-2)th term.Here, we store the number of terms in nterms. We initialize the first term to 1 and the second term to 1.

Now write the program Like this

n= int(input("Enter The terms:"))

# Here n is define number of terms

# first two terms

n1, n2 = 1, 1

count = 1

# check if the number of terms is valid

if n<= 1:

print("Please enter a positive integer")

elif n == 1:

print(n)

print(n1)

else:

print("The Fibonacci sequence are:")

while count < n:

print(n1)

nth = n1 + n2

# Now update The values

n1 = n2

n2 = nth

count += 1

You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance