Write a program to print following series:
0 1 1 2 3 5 8 1 3
Answers
Required Answer:-
Question:
- Write a program to print Fibonacci Series.
Solution:
Fibonacci Series is a series in which each term is equal to the sum of its previous two terms.
Here is the code.
n=int(input("Enter n - "))
a,b,c=1,0,0
for i in range(0,n):
print(c,end=" ")
c=a+b
a,b=b,c
Another approach.
s=[0,1]
[s.append(s[-2]+s[-1]) for i in range(int(input("Enter n: "))-2)]
print(s)
Output is attached.
Java Code:-
package Coder;
import java.util.*;
public class FibboSeries {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int a=0,b=1,c=0;
System.out.println("Enter number of terms:");
int n=sc.nextInt();
for(int i=0;i<=n;i++)
{
System.out.print(a+" ");
c=a+b;
a=b;b=c;
}
}
}
•Java Output Attached
_____________________
Python Code:-
n=(int)(input("Enter number of terms:\n"))
a,b,c=0,1,0
for i in range(1,n+1):
print(a,end=" ")
c=a+b
a=b
b=c
•Python Output Attached
____________________
QBASIC CODE:-
CLS
A=0
B=1
C=0
PRINT "ENTER NO. OF TERMS"
INPUT N
FOR I=1 TO N
PRINT A
C=A+B
A=B
B=C
NEXT I
END