code in java the first numbers of the fibonacci sequence are
code in java. the first numbers of the fibonacci sequence are 0,1,1,2,3,5,8,13,21,34,55,89,144
* each number is calculated buy adding the previous two numbers. Write a program that ask the user how mnay number of the fibonacci sequence want they want to see and then it shows the number in sequence.
*
Solution
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
int fibseqnce; //variable to store number of the fibonacci sequence
Scanner sc=new Scanner(System.in);
//asking user to enter number of the fibonacci sequence
System.out.println(\"how many number of the fibonacci sequence you want to see \");
fibseqnce=sc.nextInt();//storing user entered number of the fibonacci sequence in fibseqnce
//defining array to store fibonacci sequence
int[] fib=new int[fibseqnce];
fib[0]=0;//first index=0
fib[1]=1;//second index=1
System.out.println(\"fibonacci sequence of \"+fibseqnce+\" number :\");
System.out.print( fib[0]+\",\"+ fib[1]);
//calculating fibonacci sequence
for(int i=2;i<fibseqnce;i++){
fib[i]=fib[i-2]+fib[i-1]; //each term is the sum of previous two terms
System.out.print(\",\"+fib[i]);
}
}
}
/*******OUTPUT*******
how many number of the fibonacci sequence you want to see
8
fibonacci sequence of 8 number:
0,1,1,2,3,5,8,13
*******OUTPUT*******/
/* code has been tested on eclipse please do ask in case of any doubt,Thanks */
