Fibonacci Prime | Write a program in Java to print the Fibonacci Primes | ICSE Computer Application | ISC Computer Science



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 Fibbonacci 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 28
  14. a=y
  15. count:=0
  16. If a>1, go to step 17 or else go to step 24
  17. i=2
  18. If i<a, go to step 19 or else go to step 22
  19. If a%i==0, go to step 20 or else go to step 21
  20. count++
  21. i++. Go to step 18
  22. If count==0, go to step 23 or else go to step 24
  23. Print y+", "
  24. sum=y+z
  25. y=z
  26. z=sum
  27. Go to step 13
  28. Stop

Program

Buffered Reader

import java.io.*;
class FibonacciPrimes
{

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

    boolean isPrime(int a)
    {
        int count = 0;
        if(a>1)
        {
            for(int i=2;i<a;i++)
            {
                if(a%i==0)
                {
                    count++;
                }
            }
            if(count==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    void display()
    {
        while(y<=x)
        {
            if(isPrime(y)==true)
            {
                System.out.print(y+", ");
            }
            int sum=y+z;
            y=z;
            z=sum;
        }
    }

    void main() throws IOException
    {
        accept();
        display();
    }
}

Scanner

import java.util.*;
class FibonacciPrimes
{

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

    boolean isPrime(int a)
    {
        int count = 0;
        if(a>1)
        {
            for(int i=2;i<a;i++)
            {
                if(a%i==0)
                {
                    count++;
                }
            }
            if(count==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    void display()
    {
        while(y<=x)
        {
            if(isPrime(y)==true)
            {
                System.out.print(y+", ");
            }
            int sum=y+z;
            y=z;
            z=sum;
        }
    }

    void main() 
    {
        accept();
        display();
    }
}

Documentation

Variable Description

 Data Type Variable NameDescription
 intTo store the end value of series 
 intTo store the value to be displayed 
 intTo store the previous value of the fibonacci series 
 int sum To store the sum of previous terms of series 
 intcount To count the number of factors except one and itself 
 intTo parse a value to a method 
 intTo act as a counter variable 

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept data from the user 
 voiddisplay() To display the fibonacci primes 
 booleanisPrime(int) To check whether the argument is a prime number or not 
 voidmain() To call all the functions in the correct order 

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

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences