Fibonacci series
Code:
import java.util.Scanner;class Main{public static void main(String anyVar[]){Scanner sc=new Scanner(System.in);System.out.println("Enter any number till that yu want Fibonacci series:");int num=sc.nextInt();System.out.println("Fibonacci series till "+num+"is as follows:");int num1=1,num2=1,num3=0;for(int i=0;i<num;i++){if(i==0||i==1)System.out.print(1);else{//Fibonacci number is sum of previous two Fibonacci numbernum3=num1+num2;num1=num2;num2=num3;System.out.print(" "+num3);}}}
Output:
Enter any number till that yu want Fibonacci series:
12
Fibonacci series till 12is as follows:
1 1 2 3 5 8 13 21 34 55 89 144
Smoke:
Comments
Post a Comment