Write a Program to Print the Fibonacci Series in Java | ISC Computer Science | ICSE Computer Application
Algorithm
- Start
- x:=0
- y:=0
- z:=0
- If x<=1, go to step 6 or else go to step 11
- Print "Enter the end point of the Fibonacci Series"
- Read x
- If x<=1, go to step 9 or else go to step 10
- Print "Invalid Value. Try Again."
- Go to step 5
- y:=0
- z:=1
- If y<=x, go to step 14 or else go to step 19
- Print y+", "
- sum=y+z
- y=z
- z=sum
- Go to step 13
- Stop
Program
Buffered Reader
import java.io.*;
class Fibonacci
{
int x, y, z;
void accept() throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
while (x<=1)
{
System.out.println("Enter the end point of the Fibonacci Series");
x=Integer.parseInt(br.readLine());
if(x<=1)
{
System.out.println("Invalid Value. Try Again");
}
}
y=0;
z=1;
}
void display()
{
while(y<=x)
{
System.out.print(y+", ");
int sum=y+z;
y=z;
z=sum;
}
}
void main() throws IOException
{
accept();
display();
}
}
Scanner
import java.util.*;
class Fibonacci
{
int x, y, z;
void accept()
{
Scanner sc=new Scanner(System.in);
while (x<=1)
{
System.out.println("Enter the end point of the Fibonacci Series");
x=sc.nextInt();
if(x<=1)
{
System.out.println("Invalid Value. Try Again");
}
}
y=0;
z=1;
}
void display()
{
while(y<=x)
{
System.out.print(y+", ");
int sum=y+z;
y=z;
z=sum;
}
}
void main()
{
accept();
display();
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
int | x | To store the end limit of the series |
int | y | To store the value to be printed |
int | z | To store the previous element of the series |
int | sum | To calculate the sum of the previous two elements of the series |
Method Description
Return Type | Method Name | Description |
void | accept() | To accept values from the user |
void | display() | To display the fibbonacci series |
void | main() | To act as an entry gateway for the compiler |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @IscJavaCode
Twitter: @IscCode
Quora: ISC Java Code
Stay Healthy
Stay Happy
Stay Helpful
Comments
Post a Comment