Java program to find Fibonacci series of a given number

Friday, February 4th, 2011
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;
		}
	}
}
Avatar of surajk

Author Name :
surajk

Total : 31 Comments


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

  1. Aishwarya Basu says:

    thank u………………..it was a nice help

  2. anne says:

    can you help me how to solve this problem??its also a fibonacci series

    example input:
    first number = 3
    second number = 5
    terms = 5

    output:
    3, 8, 13, 21, 34,

  3. jai says:

    thank uuuuuuuuuu

  4. avinash says:

    how to write a java program of sorting a number with in afile?

  5. Sandip says:

    you have to improove your proper word in program…

  6. narendra says:

    u jaffa prog was not excuting…………. maintain good contents

  7. Saranya says:

    Thank u so much……its very simple to learn us….

  8. lamcy says:

    help me write a program dat will generate this fibonacci series 1,1,2,3,5,8,13

  9. Sudip says:

    thanks.

  10. ravikrishna says:

    thnq so much…………..

  11. student says:

    import java.util.Scanner;
    public class series
    {
    public static void main (String[]args){
    Scanner in = new Scanner (System.in);
    System.out.println(“Type in the number of terms”);
    int n = in.nextInt();

    if (n == 1)
    {
    System.out.println(“1″);
    return;
    }

    if (n == 2)
    {
    System.out.println(“1 1″);
    return;
    }

    int res = 1 ;
    int fnum = 1;

    System.out.print(“1 1 “);

    for (int i = 1 ; i<=n ; i++){
    int temp = res;
    res = fnum;
    fnum = res + temp;
    System.out.print(fnum + " ");
    }
    }
    }

    How do i convert this into buffered reader ??

  12. Akila says:

    Thank u so much……..it was easy to learn,for java programming.once again thank u so much…………..

  13. manojkumar kalita says:

    thnx….

  14. Firoz shah says:

    anne says:
    December 8, 2011 at 3:01 pm
    can you help me how to solve this problem??its also a fibonacciseries
    example input:
    first number = 3
    second number = 5
    terms = 5
    output:
    3, 5,8, 13, 21, 34,

    import java.io.*;
    class fibonacci
    {
    public static void main(String args[])
    {
    DataInputStream dis=new DataInputStream(System.in);
    System.out.println(“ENTER VALUE OF n:”);
    int n=Integer.parseInt(dis.readLine());
    int f1,f2,f3;
    f1=3;
    f2=5;
    System.out.println(f1 “\t” f2 “\t”);
    for(int i=2;i<=n;i )
    {
    f3=f1 f2;
    System.out.println(f3 "\t");
    }
    f1=f2;
    f2=f3;
    }
    }

  15. Baishali Das Adhikari says:

    0,1,1,2,3,5,8…144
    how cn we convert this ??

  16. vijaya says:

    very useful!!

  17. siva says:

    thank u so much…

  18. Kshitij Tandon says:

    Thnks a lot:)

  19. bhuvanya says:

    Input Format:

    The input consists of a single integer which corresponds to ‘n’

    Output Format:

    Output consists of a single integer which corresponds to the the nth flibonakki number.

    Sample Input:

  20. bhuvanya says:

    plss send me fibbnoci program to the above description

  21. Adnan Azmat says:

    Try this way also:
    class FibbbSeriesssprevious3
    {
    public void input(int n)
    {
    System.out.print(0+”,”+1);
    int a=0,i=1,p=0;
    while(i<n)
    {
    a=a+i+p;
    if(a<=n)
    System.out.print(","+a);
    i=i+a+p;
    if(i<=n)
    System.out.print(","+i);
    p=i+p;
    if(p<=n)
    System.out.print(","+p);
    }}}

  22. Adnan Azmat says:

    Sorry I posted a wrong program,
    this is the correct one:
    class FibbbSeriesssprevious2
    {
    public void input(int n)
    {
    int a=0,i=1;
    while(a<n)
    {
    if(a<=n)
    System.out.print(a+",");
    a=a+i;
    if(i<=n)
    System.out.print(i+",");
    i=i+a;
    }}}

  23. kamal says:

    public class Fibonacci{
    public static void main(String args[]){
    int a=0;
    int b=1;
    int temp=0;
    for(int i=1; i<=15; i++){
    System.out.println(temp+"="+"fibonacci");
    a=b;
    b=temp;
    temp=a+b;
    }
    }
    }

  24. aaa says:

    help me write a program dat will generate this fibonacci series 1,1,2,3,5,8,13

    public class Fibonacci
    {
    public static void main(String args[])
    {
    int i=0;
    int j=1;
    int num=0;

    for(int a=0; a<=7; a++)
    {
    System.out.print(num+" ");
    i=j;
    j=num;
    num=i+j;
    }
    }
    }

  25. manish kaushik says:

    import java.lang.*;
    import java.util.*;

    class One
    {
    public static void main(String args[])
    {
    int [] num={12,25,47,85,45,58,56,55,89,81,2,78};
    Arrays.sort(num);
    for(int k[].num);
    System.out.println(k);
    }
    }

    this program is used to sort elements.

  26. manish kaushik says:

    import java.lang.*;
    import java.util.*;

    class One
    {
    public static void main(String args[])
    {
    int [] num={12,25,47,85,45,58,56,55,89,81,2,78};
    Arrays.sort(num);
    System.out.println(Arrays.toString(num));
    }
    }

  27. manish kaushik says:

    import java.lang.*;
    import java.util.*;
    class Two
    {
    public static void main(String args[])
    {
    int prev,next,sum,n;
    prev=next=1;
    for(n=0;n<=10;n++)
    {
    System.out.println(prev);
    sum=prev+next;
    prev=next;
    next=sum;
    }
    }
    }

    this program is used to print 1,1,2,3,5,8,13….

  28. soumya says:

    this code not enough to print the sreies like 011235…….

  29. XCata says:

    public class fib2
    {
    public static void main(String[] args)
    {
    int maxIndex = 20, fnum = 0, snum = 1, tnum;

    for(int i = 0; i < maxIndex; i++)
    {
    System.out.println(fnum);
    tnum = snum+fnum;
    fnum = snum;
    snum = tnum;
    tnum = snum;
    }
    }
    }

  30. XCata says:

    Here’s a simplified version. Cheers

    import java.util.*;
    class fib3
    {
    public static void main(String[] args)
    {
    System.out.println(“Enter fibonacci number: “);
    Scanner scan = new Scanner(System.in);
    int input = scan.nextInt();
    int fnum = 0, snum = 1, subsequent;

    for(int i = 0; i<input; i++)
    {
    System.out.println(fnum);
    subsequent = fnum + snum;
    fnum = snum;
    snum = subsequent;
    }
    }
    }

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup

Email: