Java program to find Fibonacci series of a given number

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

67 thoughts on “Java program to find Fibonacci series of a given number

  1. 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 ??

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

  3. 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;
    }
    }

  4. 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:

  5. 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);
    }}}

  6. 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;
    }}}

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

  8. 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;
    }
    }
    }

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

  10. 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));
    }
    }

  11. 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….

  12. 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;
    }
    }
    }

  13. 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;
    }
    }
    }

  14. /*
    nth fibonacci number
    */
    public static int nthFibonacciNumber(int nTh){
    int a0=0;
    int a1=1;
    if(nTh<=0){ // Check for negative number
    throw new IllegalArgumentException(nTh+" : nth number does not excite");
    }
    if(1==nTh){ //first Number
    return a0;
    }else if(2==nTh){ //Second Number
    return a1;
    }else{ //reset of serious
    for(int n=2;n<nTh;n++){
    a1=a0+a1;
    a0=a1-a0;
    }
    }
    return a1;
    }

  15. can you write a program for generating the fibonacci series without using the loops and recursive function.

  16. //to print fibinocci series upto 20 no.
    class test22 {

    public static void main(String args[]) {

    int n=20;

    System.out.println(“\n\nFibonacci series upto ” +n +” numbers : “);
    for(int i=1; i<=20; i++){
    System.out.print(fibonacciRecusion(i) +" ");
    }
    }

    public static int fibonacciRecusion(int n){
    if(n == 1 || n == 2){
    return 1;
    }

    return fibonacciRecusion(n-1) + fibonacciRecusion(n-2);
    }

    public static int fibonacciLoop(int n){
    if(n == 1 || n == 2){
    return 1;
    }
    int fibo1=1, fibo2=1, fibonacci=1;
    for(int i= 3; i<= n; i++){
    fibonacci = fibo1 + fibo2;
    fibo1 = fibo2;
    fibo2 = fibonacci;

    }
    return fibonacci;
    }
    }

  17. 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;
    }

    }}

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

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

  20. 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);
    }
    }

  21. 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;
    }
    }
    }

  22. Who can make a program checker Fibonacci or not with the array input from the keyboard in java language? I need help.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in