Fibonacci Prime Series in JAVA

Fibonacci Prime Series

Fibonacci Series : A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 0, 1, 1, 2, 3, 5, 8, etc.
 
Fibonacci Prime Series : A Fibonacci prime is a Fibonacci number that is prime, a type of integer sequence prime.
 
The first few terms of the sequence are :-
 
2, 3, 5, 13, 89, 233, 1597, 28657, 514229, 433494437, 2971215073, ....


Program to Print Fibonacci Prime Series given below :-


Program -1 :-

import java.io.*;
import java.util.*;
class Fibonacci_Prime
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int a=0,b=1,c=0,n,i,j,k=0;
        System.out.println("Enter Range");
        n= in.nextInt();
        System.out.println("Fibonacci Series given below:-");
        for(i=1;i<=n;i++)
        {
            c=a+b;
            if(c>n)
            {
                break;
            }
            else
            {
                for(j=1;j<=c;j++)
                {
                    if(c%j==0)
                    {
                        k++;
                    }
                }
                if(k==2)
                {
                    System.out.print(c+"\t");
                }
                a=b;
                b=c;
                k=0;
            }
        }
    }//end of main method
}//end of class
 

 

Program -2 :-

import java.io.*;
import java.util.*;
class Fibonacci_Prime1
{
    public static void main(String args[])
    {
        Scanner in = new Scanner(System.in);
        int a=0,b=1,c=0,n,i,j,k=0;
        System.out.println("Enter Number of Terms");
        n= in.nextInt();
        System.out.println("Fibonacci Series given below:-");
        System.out.print(a+"\t");//printing first number
        System.out.print(b+"\t");//printing second number
        c=a+b;//printing third term
        a=b;
        b=c;
        System.out.print(c+"\t");//printing second number
        for(i=4;i<=n;i++)
        {
            c=a+b;
            {
                for(j=1;j<=c;j++)
                {
                    if(c%j==0)
                    {
                        k++;
                    }
                }
                if(k==2)
                {
                    System.out.print(c+"\t");
                }
                a=b;
                b=c;
                k=0;
            }
        }
    }//end of main method
}//end of class


For Proper Understanding Watch the Video :-




Watch this video : Fibonacci Prime Series
 
Follow me on Instagram

All the best :)
Keep Learning :)

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Smith Number in JAVA

Trimorphic Number in JAVA