Use Python 3 for the following task: The Fibonnaci sequence is a sequence of int
ID: 3585420 • Letter: U
Question
Use Python 3 for the following task:
The Fibonnaci sequence is a sequence of integers, beginning with 1, 1, 2, 3, 5, ..., wherein each element is the sum of the previous two elements (except for the first two elements, which are defined as 1 and 1). Write a program to prompt a user to enter a number, n. Then, print the first n Fibonacci numbers. Note: We haven't yet discussed writing our own functions, so please do not submit a recursive-function solution. Example Output: Please enter a number: 7 1 2 5 13Explanation / Answer
1. Accept the input with the following kind of statement
n=input("Enter a number:")
2. Initialize first 2 numbers to 1
f1=1
f2=1
3. Write a loop statement for computing ith Fibonacci no,
fi = f1 + f2 initially
4. Then keep advancing f1 and f2.
So the code will be something like this:
print "Fibonnaci Numbers "
n=input("Enter a number:")
f1=1
f2=1
print " "
print (f1)
print (f2)
for i in range (n-2):
fi=f1+f2
print(fi)
f1=f2
f2=fi
5. As first 2 numbers are already printed outside the loop, the loop is needed for n-2.
6. Example 1:
7. Example 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.