Fibonacci Series - Count Odd & Even
Fibonacci Series - Count Odd & Even TCS NQT
Problem:
The program must accept an integer N as the input. The program must print the Fibonacci series until the given integer N. Then the program must print the number of odd integers present in the Fibonacci series as the output. Finally, the program must print the number of even integers (except 0) present in the Fibonacci series as the output.
Boundary Condition(s):
2 <= N <= 10^8
Input Format:
The first line contains N.
Output Format:
The first line contains the Fibonacci series until the given integer N. The second line contains the number of odd integers present in the Fibonacci series. The third line contains the number of even integers present in the Fibonacci series.
Example Input/Output 1:
Input:
25
Output:
0 1 1 2 3 5 8 13 21
6
2
Explanation:
Here N=25, the Fibonacci series until 25 is given below. 0 1 1 2 3 5 8 13 21 The number of odd integers in the series is 6 and the number of even integers (except 0) in the series is 2. Hence the output is 0 1 1 2 3 5 8 13 21 6 2
Example Input/Output 2:
Input: 89
Output:
0 1 1 2 3 5 8 13 21 34 55 89
8
3
Program:
n = int(input())
l = [0,1,1]
while l[-1]+l[-2] <= n:
l.append(l[-1]+l[-2])
print(*l)
e,o = -1,0
for i in l:
if i%2==0:
e += 1
else:
o += 1
print(o,e,sep='\n')
Comments
Post a Comment