Java program to find Fibonacci series of a given number

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

Java program to find Fibonacci series of a given number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*Write a program to find Fibonacci series of a given no.
  Example :
  Input - 8
  Output - 1 1 2 3 5 8 13 21
 */
class Fibonacci{
	public static void main(String args[]){
		int num = Integer.parseInt(args[0]); //taking no. as command line
		argument.
			System.out.println("*****Fibonacci Series*****");
		int f1, f2=0, f3=1;
		for(int i=1;i<=num;i++){
			System.out.print(" "+f3+" ");
			f1 = f2;
			f2 = f3;
			f3 = f1 + f2;
		}
	}
}

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

55 Responses to “Java program to find Fibonacci series of a given number”

  1. For all those who need this prog to print fibonacci series till Nth no. Here it is.

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

    class fibonacci
    {
    public static void main(String args[])throws IOException
    {
    BufferedReader br= new BufferedReader (new InputStreamReader(System.in));

    int a, n, b;
    System.out.print(” Enter the no till which you want the series: “);
    n= Integer.parseInt(br.readLine());

    System.out.println(“The Fibonacci series is: “);
    a=0;
    b=1;
    System.out.println(a);
    System.out.println(b);

    for(int c=1; c<n;)
    {
    c=a+b;
    if(c<=n)
    {
    System.out.println(c); }
    a=b;
    b=c;
    }

    }}

    Reply
  2. Crystal Adams

    The site has a good program but it is for higher level students. I am only in class 9 and i got the help i needed from one of those who left a comment. His name is Adnan Azmat. His program is much simpler. But, the program in this site is definitely good, but a little hard for me. Don’t be offended please.

    Reply
  3. nolasco orena

    create a java programs called fibonacci that will display the first 10 fibonacci # f(n) ,where f=(n)+f(n-1)+f(n-2) and f(1)=f(2)=1
    display also the average of ten number .
    the output would be similiar to figure 1

    Reply
  4. import java.io.*;
    public class fibo
    {
    public static void f(int a)
    {
    int i=0,j=1,k;
    System.out.println(“\n”+i+”\n”+j);
    for(k=0;k<a;k++)
    {
    int l;

    l=i+j;
    System.out.println("\n"+l);
    i=j;
    j=l;
    }
    }
    public static void main(String args[])throws Exception
    {
    fibo s=new fibo();
    s.f(10);
    }
    }

    Reply
  5. shresthaa atreya

    public class series
    {
    public static void main(String ar[])
    {
    int a=0,b=1;
    Int n=Integer.parseInt(ar[0]);
    System.out.println(a+” “+b);
    for(i=1;i<=n-2;i++)
    {
    int c=a+b;
    System.out.print(" "+c);
    a=b;
    b=c;
    }
    }
    }

    Reply
  6. if u will provide output also this is better for some ordinary programmer

    Reply

Leave a Reply