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
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
hi need help in reading a names from a text file in the given format
ReplyDeletemiddle name first name lastname
Also help me with more programs on operations on files
Please see the posting on October 27-2010
ReplyDeletethx dude
ReplyDeletei needed dis 4 my exam 2morrow
Ok. Mannu
ReplyDeletepls can u give a program for armstrong number using recursive?
ReplyDeletePlease check today's post
DeleteSir,
ReplyDeleteAre non-static methods more preferred over static methods?
It depends on situation. In certain stage static functions are needed.
Delete