Write a Program to Print the Fibonacci Series in Java | ISC Computer Science | ICSE Computer Application



Algorithm

  1.   Start
  2. x:=0
  3. y:=0
  4. z:=0
  5. If x<=1, go to step 6 or else go to step 11
  6. Print "Enter the end point of the Fibonacci Series"
  7. Read x
  8. If x<=1, go to step 9 or else go to step 10
  9. Print "Invalid Value. Try Again."
  10. Go to step 5
  11. y:=0
  12. z:=1
  13. If y<=x, go to step 14 or else go to step 19
  14. Print y+", "
  15. sum=y+z
  16. y=z
  17. z=sum
  18. Go to step 13
  19. 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 TypeVariable Name Description 
 intTo store the end limit of the series 
 intTo store the value to be printed 
 intTo store the previous element of the series 
 int sum To calculate the sum of the previous two elements of the series 

Method Description

 Return TypeMethod Name Description 
 voidaccept() To accept values from the user 
 voiddisplay() To display the fibbonacci series 
 voidmain() To act as an entry gateway for the compiler

Output

Image is Unavailable


Image is Unavailable

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
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Emirp Number | Write a program in Java to print an Emirp Number. | ISC Computer Science | ICSE Computer Applications

Conversion of Complex Sentences to Compound Sentences