Fibonacci Java program

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number(called a Fibonacci number) is equal to the sum of the preceding two numbers.


Fibonacci sequence, Fibonacci java program,techmacron
Program

import java.io.*;
class fibonacci {
public static void main(String args[])throws IOException{
int fib,a=0,b=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the limit :");
int lim=Integer.parseInt(br.readLine());
System.out.print("Fibonacci Numbers : "+a+" "+b+" ");
for(int i=0;i<lim-2;i++)
{
fib=a+b;
a=b;
b=fib;
System.out.print(fib+" ");
} }
}

Output

Fibonacci, java program, output, techmacron



Comments