Write a program to print Fibonacci series up to n term.

//Write a program to print the Fibonacci series.
class Fib {
    public static void main(String args[ ])
         {
        int n=Integer.parseInt(System.console().readLine("enter the no. n="));
        int i,a=1,b=1,c=0;
        System.out.println("fibnoci series is:");
       
        System.out.print(a);
        System.out.print(" "+b);
        for(i=0;i<=n;i++)
                {
            c=a+b;
            a=b;
            b=c;
            System.out.print(" "+c);
        }
        System.out.println();
        System.out.print("th value of the series is: "+c);
    }
}

2 comments:

  1. the fibonacci series what ever you are printing should not exceed a particular limit
    how to do this.
    for example 0 ,1, 1, 2, 3, 5, 7, 12,19
    so it should stop at 19 ..means it shud take input as 19 instead of 9..
    how to do this

    ReplyDelete
    Replies
    1. import java.util.Scanner;

      public class fibonacci {

      public static void main(String[] args)
      {
      // TODO Auto-generated method stub
      int i=0,f=1,n,t;
      Scanner s=new Scanner(System.in);
      n=s.nextInt();
      System.out.print(i+"\t");
      while(f<=n)
      {
      System.out.print(f+"\t");
      t=f;
      f=i+f;
      i=t;
      }
      }

      }

      Delete