Thursday, October 14, 2010

Fibonacci numbers Using Recursive Function and its Algorithm

BlueJ program to display fibonacci numbers using recursive function.

import java.io.*;
class BlueJRecursive
{
public void show(int p,int c,int n)
{
int next;
if(n==0)
return;
next=p+c;
System.out.print(" "+next);
p=c;
c=next;
show(p,c,n-1);
}

public static void main(String args[])throws IOException
{
BlueJRecursive ob=new BlueJRecursive();
int p=0,c=1,n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of elements in the series:");
n=Integer.parseInt(br.readLine());
System.out.print("The Fibonacci series is= "+p+" "+c);
ob.show(p,c,n-2);
}
}


Algorithm of this program is as follows:

Step 1: Take variables ‘p’,’c’,’n’ of int type.
Step 2: Initialize ‘p’ with 0 and ‘c’ with 1.
Step 3: Take the number of elements in the series in ‘n’
Step 4: Display ‘p’ and ‘c’.
Step 5: Repeat upto Step 11 until ‘n’ becomes 0
Step 6: Assign the sum of ‘p’ and ‘c’ on another variable ‘next’
Step 7: Display next
Step 8: Assign the value of ‘c’ on ‘p’
Step 9: Assign the values of ‘next’ on ‘c’
Step 10: Reduce ‘n’ by 1
Step 11: Again go to Step 5.

Back To BlueJ Series Display Programs Home Page: CLICK HERE

8 comments:

  1. hi need help in reading a names from a text file in the given format
    middle name first name lastname
    Also help me with more programs on operations on files

    ReplyDelete
  2. Please see the posting on October 27-2010

    ReplyDelete
  3. thx dude
    i needed dis 4 my exam 2morrow

    ReplyDelete
  4. pls can u give a program for armstrong number using recursive?

    ReplyDelete
  5. Sir,
    Are non-static methods more preferred over static methods?

    ReplyDelete
    Replies
    1. It depends on situation. In certain stage static functions are needed.

      Delete

Subscribe via email

Enter your email address:

Delivered by FeedBurner