Prime Fibonacci Numbers in Java

Write a program in Java to allow the user to enter the values of M and N such that M < N.

Now display all the Prime Fibonacci numbers in the range M to N (both inclusive).

Prime Fibonacci numbers are the ones that are prime and also appear in the Fibonacci series.

A Fibonacci Series is a series in which the first two terms are 0 and 1. The next term is always the sum of the previous two terms.

Example: 0, 1, 1, 2, 3, 5, 8, …

A prime number is a positive integer that has only two factors: one and itself.

Example: 2, 3, 5, 7, 11, 13, …

import java.io.*;
class PrimeFibonacci{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("M = ");
        int m = Integer.parseInt(br.readLine());
        System.out.print("N = ");
        int n = Integer.parseInt(br.readLine());
        if(m >= n){
            System.out.println("Invalid range!");
            return;
        }
        for(int i = m; i <= n; i++){
            if(!isPrime(i))
                continue;
            int num = 1;
            int result = fibo(num);
            while(result < i){
                num++;
                result = fibo(num);
            }
            if(result == i)
                System.out.print(i + "\t");
        }
        System.out.println();
    }
    public static boolean isPrime(int num){
        int f = 0;
        for(int i = 1; i <= num; i++){
            if(num % i == 0)
                f++;
        }
        if(f == 2)
            return true;
        return false;
    }
    public static int fibo(int num){
        if(num == 1)
            return 0;
        else if(num == 2)
            return 1;
        return fibo(num - 1) + fibo(num - 2);
    }
}

3 thoughts on “Prime Fibonacci Numbers in Java

  1. Design a class Change to perform string related operations. The details of the class are given below:
    Class name : Change
    Data members
    str : stores the word newstr : stores the changed word
    len : stores the length of the word Member functions
    Change() : default constructor void inputword() : to accept a word
    char caseconvert(char ch) : converts the case of the character and returns it
    void recchange(int) : extracts characters using recursive technique and changes its case using caseconvert() and forms a new word void display() : displays both the words
    Specify the class Change, giving details of the constructor(), member functions void inputword(), char caseconvert(char ch), void recchange(int) and void display(). Define the main function to create an object and call the functions accordingly to enable the above change in the given word.

      • Write a program in Java to accept a natural number N.Now display the twin prime numbers before and after N.
        Also display the nearest twin prime numbers from N.
        Eg:
        N=34
        Twin Prime numbers before 34 are:29,31
        Twin Prime numbers after 34 are:41,43
        Nearest twin prime numbers from 34 are:29,31

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.