Write a program to print the Fibonacci series.
( Fibonacci series:- 0,1,1,2,3,5,8,13,21…….)
plz answer me
Answers
Answer:
The given code is written in Java.
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int a=1,b=0,c=0,n,i;
System.out.print("Enter limit: ");
n=sc.nextInt();
for(i=1;i<=n;i++) {
System.out.print(c+" ");
c=a+b;
a=b;
b=c;
}
sc.close();
}
}
Interesting facts about this series:
- The fibonacci series is a series of numbers where each term in the sequence is equal to sum of previous two terms.
- The first terms are 0 and 1. So, the next term will be 1.
- Every 3rd term in this sequence is a multiple of 2.
- Every 4th term is a multiple of 3.
- Every 5th term is a multiple of 5 and so on.
Refer to the attachment for output.
•••♪
Answer:
Program:-
import java.util.*;
public class Fibonacci
{
public static void main(String args[ ])
{
Scanner in=new Scanner(System.in);
int n,a=0,b=1,c=0,i=1;
System.out.println("Enter the number of terms that you want to display");
n=in.nextInt();
System.out.print(a + " ");
System.out.print(b+ " ");
while(i<=n)
{
c=a+b;
System.out.print(c+ " ");
a=b;
b=c;
i++;
}
}
}
- The first photo is the program
- The second photo is the number of terms till you want to display I entered n=10
- The 3rd photo is the output