For Loop

# Fibonacci Series Java Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

• Anonymous says:

Oh man I’m trying to figure this out for so many nights but still can’t…
Well think I’m lucky enough to accidentally enter at your site.

Thanks!!
^_^

• Anonymous says:

can u make program in Fibonacci using array program in java that will input numbers….

• Anonymous says:

Im looking for the same thing!
I tried on my own, but i cant get it to work, any tips?

• Anonymous says:

this program is right

• Anonymous says:

import java.io.*;
class Fibon
{
public static void main(String args[])
{
int i,n;
Console con=System.console();
System.out.print(“enter how many fibonacci no

u want to print “);
long a[]=new long[n];
a=0;
a=1;
for(i=2;i<n;i++) {=”” a[i]=”a[i-1]+a[i-2];” }=”” system.out.println(“fibonacci=”” series=”” upto=”” “=”” +=”” n);=”” for(=”” i=”0;” i<=”” n;=”” i++){=”” system.out.print(a[i]=”” +=”” “=”” “);=”” }=”” }=”” }=””>

• Anonymous says:

import java.io.*;

public class Fibanocci
{
public static void main(String args[])
{
try
{
//Getting limit from the user…
System.out.println(“Enter the Limit for fibanocci : “);
int limitOfFib = Integer.parseInt(limitOfFibObject.readLine());

//hard coding the limit…
//int limitOfFib = 5;

int[] fibSeries = new int[limitOfFib];

fibSeries=0;
fibSeries=1;

for(int i=2; i<limitoffib ;=”” i++)=”” {=”” fibseries[i]=”fibSeries[i-1]+fibSeries[i-2];” }=”” system.out.println(“fibonacci=”” series=”” till=”” “+limitoffib);=”” for(int=”” i=”0;” i<limitoffib;=”” i++)=”” {=”” system.out.print(fibseries[i]=”” +=”” “=”” “);=”” }=”” }=”” catch(exception=”” e)=”” {=”” system.out.println(“exception=”” is:”+e);=”” }=”” }=”” }=””>

• Anonymous says:

import java.io.*;
import java.util.*;

public class Fibonacci {
public static void main(String[] args) throws Exception {
System.out.print(“Enter value of n: “);
int num = Integer.parseInt(st);
int f1=0,f2=0,f3=1;
for(int i=1;i<=num;i++){
System.out.println(f3);
f1=f2;
f2=f3;
f3=f1+f2;
}
}
}

• Anonymous says:

your program calculating 19 numbers only please modify.

• Anonymous says:

can you create a program using fibonacci formula ..
the output should be like this

first : 1
Second: 3
Limits : 5

out pout is 1,3,4,7,11

plese help me 🙂

• Pal Raj says:

int result = 0, oldVal = 0, newVal =1, limit =5, temp=0;

while(newVal < limit) {
temp = oldVal;
result = newVal + oldVal;
oldVal = newVal;
newVal =result;
System.out.println(“Fib (“+ temp +” + “+ oldVal +”) -> ” + result);
}

• Anonymous says:

this program is absolutely right.

• Jinky Seguiran says:

nice easy to understand huh!

• ramu says:

cool man i love you!!

• Rohit5 says:

Here is recursive way of generating Fibonacci Series in Java , its a natural example of recursion.

• Jay Abuzo says:

for my assignment..cool man !!

• Jay Abuzo says:

any other types of loop?

• teona says:

I like it’s very good

• Shyrelljade chin says:

Wow!
Thanks you for this example.

• Shirayuki Hime says:

java program that will accept 2 numbers and print out in fibonacci serries:
example:
enter 1st term:
3
enter 2nd term:
5
output.
3 3 6 9 15

• Prasanna Reddy says:

int f1, f2, f3;
f1=f2=3;
System.out.println(f1);
System.out.println(f2);
int i=1;
while(i<=3)
{
f3=f1+f2;
System.out.println(f3);
f1=f2;
f2=f3;
i++;

• Tomas Pesek says:

Another way without using an array. Nothing wrong with the array, just for practise.

for(int i=0, j=1, k=1; k<4182; k = j + i){
if(i==0)
System.out.print(i + ” ” + j + ” “);
System.out.print(k + ” “);
i = j;
j = k;
}

• Kritika Gupta says:

To add numbers of series

• Tulasiram says:

how and which purpose we are using %

• Charl Broekman says:

For user input Fibonacci series:

import java.util.Scanner; // user input as well as output
public class MyFibonacci
{
public static void main(String args[]) // main method
{
//declare and initialise variable
int febCount;
febCount = 0;
Scanner input = new Scanner(System.in); // create Scanner
System.out.print( “How much fibonacci numbers do you want?” ); // user input
febCount = input.nextInt(); // store input in febCount
// array of frequency counters
int[] feb = new int[febCount];
// create first two series elements
feb = 0;
feb = 1;
//create the Fibonacci series and store it in an array
for(int i=2; i < febCount; i++)
{
feb[i] = feb[i-1] + feb[i-2];
}
for(int i=0; i< febCount; i++)
{
System.out.printf(“%8d%10d%n”, i+1, feb[i] ); // displays output vertically
}
}
}

• jade says:

can anyone help me for this question: Write a program that reads in an arbitrary number between
1 and 100, then calls a function check_fib that checks if the number is
part of the Fibonacci series. The main () program should then print:
<> is/is not part of the Fibonacci series

• Anonymous says:

If you want to use arrays to display the fibonacci series,how should we implement? i got the solution using loops, but what for arrays? Please help

int sum=0;
int count;
int i=1;
int num1 = 0, num2 = 1;
System.out.print(“How may numbers you want in the sequence:”);
Scanner x = new Scanner(System.in);
count = x.nextInt();
System.out.println(“The fibonacci series of “+count+” is : “);

while(i<=count)
{
System.out.print(num1+" ");
sum = num1 + num2;
num1 = num2;
num2 = sum;
i++;
}

• JB says:

Hello,
I didn’t understand this line:

series[i] = series[i-1] + series[i-2];

Thanks!

• Suyash says:

But n=1 ke liye nhi chalega