For Loop

Fibonacci Series Java Example

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

26 Comments

  • 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.

    I already add your site to my favorites.
    Thanks!!
    ^_^

      • 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 “);
        n=Integer.parseInt(con.readLine());
        long a[]=new long[n];
        a[0]=0;
        a[1]=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]=”” +=”” “=”” “);=”” }=”” }=”” }=””>

      • import java.io.*;

        public class Fibanocci
        {
        public static void main(String args[])
        {
        try
        {
        //Getting limit from the user…
        BufferedReader limitOfFibObject = new BufferedReader(new InputStreamReader(System.in));
        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]=0;
        fibSeries[1]=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);=”” }=”” }=”” }=””>

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

        public class Fibonacci {
        public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(“Enter value of n: “);
        String st = reader.readLine();
        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;
        }
        }
        }

  • 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 🙂

    • 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);
      }

  • how about this program can you create.
    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

    • 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++;

  • 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;
    }

  • 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] = 0;
    feb[1] = 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
    }
    }
    }

  • 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

  • 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++;
    }

Sponsors

Facebook Fans