How to write a python program for the Fibonacci series and need to show series in Pyramid?
Question:
I want to write a Program for the Fibonacci series and need to show series in Pyramid.
Enter (through command line) the number of times the Fibonacci has to iterate.
Ex:
Enter the number of times
6
Fibonacci Series of the number is: 0 1 1 2 3
Below is the expected output –
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
I have tried this code as below –
n= int(input("enter the number of rows: "))
a=0
b=1
for i in range(a,n):
a=0
b=1
print(b,end="")
for j in range(a,i-1):
c=a+b
print(c,end="")
a=b
b=c
print()
But this is giving below output
1
1
11
112
1123
11235
the above output starting from "1" but the expected output should start from "0"
Please help me with correct python code as expected
Thanks
Answers:
There is some small change that your code will require. You have to make end=" ", so that it will print space there.
n= int(input("enter the number of rows: "))
a=0
b=1
for i in range(a,n):
a=0
b=1
print(a, b,end=" ")
for j in range(a,i-1):
c=a+b
print(c,end=" ")
a=b
b=c
print()
Try:
n = int(input("Enter the number of rows: "))
fib = []
for i in range(n):
fib.append(fib[-2] + fib[-1] if i > 1 else i)
print(' '.join(str(x) for x in fib))
Output:
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
In your code, you are computing the Fibonacci sequence from zero again and again for each row, which is redundant. Instead, you can make a list and add one item at each iteration.
I’ve used join
to insert a blank between entries, which I believe is more "pythonic."
It often makes for simpler code if you separate computation from doing pretty output. So construct your series first:
>>> series = [0,1]
>>> n = 6
>>> while len(series) < n:
series.append(series[-1]+series[-2])
>>> series
[0, 1, 1, 2, 3, 5]
Then do the output:
>>> sseries = [str(s) for s in series]
>>> sseries
['0', '1', '1', '2', '3', '5']
>>> for row in range(len(sseries)+1):
print (" ".join(sseries[:row]))
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
#include <stdio.h>
int main()
{ int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Got this answer from Tap Academy
I want to write a Program for the Fibonacci series and need to show series in Pyramid.
Enter (through command line) the number of times the Fibonacci has to iterate.
Ex:
Enter the number of times
6
Fibonacci Series of the number is: 0 1 1 2 3
Below is the expected output –
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
I have tried this code as below –
n= int(input("enter the number of rows: "))
a=0
b=1
for i in range(a,n):
a=0
b=1
print(b,end="")
for j in range(a,i-1):
c=a+b
print(c,end="")
a=b
b=c
print()
But this is giving below output
1
1
11
112
1123
11235
the above output starting from "1" but the expected output should start from "0"
Please help me with correct python code as expected
Thanks
There is some small change that your code will require. You have to make end=" ", so that it will print space there.
n= int(input("enter the number of rows: "))
a=0
b=1
for i in range(a,n):
a=0
b=1
print(a, b,end=" ")
for j in range(a,i-1):
c=a+b
print(c,end=" ")
a=b
b=c
print()
Try:
n = int(input("Enter the number of rows: "))
fib = []
for i in range(n):
fib.append(fib[-2] + fib[-1] if i > 1 else i)
print(' '.join(str(x) for x in fib))
Output:
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
In your code, you are computing the Fibonacci sequence from zero again and again for each row, which is redundant. Instead, you can make a list and add one item at each iteration.
I’ve used join
to insert a blank between entries, which I believe is more "pythonic."
It often makes for simpler code if you separate computation from doing pretty output. So construct your series first:
>>> series = [0,1]
>>> n = 6
>>> while len(series) < n:
series.append(series[-1]+series[-2])
>>> series
[0, 1, 1, 2, 3, 5]
Then do the output:
>>> sseries = [str(s) for s in series]
>>> sseries
['0', '1', '1', '2', '3', '5']
>>> for row in range(len(sseries)+1):
print (" ".join(sseries[:row]))
0
0 1
0 1 1
0 1 1 2
0 1 1 2 3
0 1 1 2 3 5
#include <stdio.h>
int main()
{ int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Got this answer from Tap Academy