Fibonacci series is 1,1,2,3,5,8,13,21,34,55,etc..
Here simple logic is Take the first number a=1,b=1, and do sum of that c=a+b.
and after that replace the value of a and b.
Lets look at the code.
Here simple logic is Take the first number a=1,b=1, and do sum of that c=a+b.
and after that replace the value of a and b.
Lets look at the code.
import java.util.Scanner; public class Fibonaci { /* 1 1 2 3 5 8 13 21 34 * * * */ public static void main(String[] args) { int a=1; int b=1; int c = 0; Scanner sc=new Scanner(System.in); System.out.println("enter the number till fibonaci series you want to print: "); int num=sc.nextInt(); System.out.print(a+" "+b); while(c<num) { c=a+b; System.out.print(" "+c+" "); a=b; b=c; } } }
No comments:
Post a Comment