0% found this document useful (0 votes)
8 views

Assignment 6

Uploaded by

Aryan Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assignment 6

Uploaded by

Aryan Jaiswal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT 6: (30.04.

2024)
Design a class FiboPrime which will display all the Fibonacci no.s upto nth
term which have atleast one prime digit in the no.
For ex: 2,3,5,13,21 are the examples of Fibonacci no. having atleast one
prime digit in it.
Class Name: FiboPrime
Data Members: int n (No. of terms)
Member Functions:
FiboPrime(int): Parameterized constructor method
int fibo (int n): Returns the nth fibonacci no. using recursive technique
void display (FiboPrime): Display all the fibonacci no.s upto n terms which
have atleast one digit as prime
boolean isprime (int p, int i): Return true or false if p is either prime no. or
not using recursive technique
Algorithm:
1. Class Definition and Setup
 Define a class named FiboPrime with:

o Data Member:

 n: integer representing the term up to which Fibonacci

primes will be checked.


2. Constructor FiboPrime(int num)
 Purpose: Initialize n and check if it’s a valid input.

 Steps:

1. Assign n the value of num.


2. If n is less than 3:
 Print "INVALID OUTPUT!" to indicate that the term is too

small.
 Exit the program.

3. Method display()
 Purpose: Display Fibonacci numbers that contain at least one prime

digit, up to the nth term.


 Steps:

1. Print a header message: "The Fibonacci Prime numbers till n


terms are:".
2. Loop i from 2 to n (starting from 2, since the 0th Fibonacci
number is not relevant here):
 Call fibo(i) to calculate the ith Fibonacci number and store

the result in s.
 While s is greater than 0:

 Extract the last digit of s by computing s % 10 and store

it in p.
 Call isprime(p, 2) to check if p is a prime number.

 If isprime returns true, print the ith Fibonacci

number.
 Break out of the loop as a prime digit has been

found in this Fibonacci number.


 If p is not prime, remove the last digit from s by
performing integer division (s /= 10) and continue the
check on the remaining digits.
4. Method fibo(int n)
 Purpose: Calculate the nth Fibonacci number using recursion.

 Steps:

1. If n is 0 or 1, return n as it directly represents the nth Fibonacci


number.
2. Otherwise, return the sum of fibo(n - 1) and fibo(n - 2).
 This is the Fibonacci formula, where each number is the sum

of the two preceding numbers.


5. Method isprime(int n, int i)
 Purpose: Check if a number n is prime using recursion.

 Steps:

1. If n is less than or equal to 2:


 Return true if n is 2 (since 2 is prime), otherwise return false.

2. If n is divisible by i, return false because n is not prime.


3. If i * i is greater than n, return true since no divisor exists beyond
the square root.
4. Otherwise, recursively call isprime(n, i + 1) to continue checking
for divisibility with the next integer.
6. Main Method
 Purpose: Execute the program by accepting input and displaying the

Fibonacci prime series.


 Steps:

1. Create a Scanner object to accept user input.


2. Prompt the user to enter a value for n1 (the nth term).
3. Create an instance ob of the FiboPrime class, passing n1 as an
argument.
4. Call ob.display() to print Fibonacci numbers up to n terms that
contain at least one prime digit.
Variable Listing Table:

Variable Type Scope Purpose


n int Instance Stores the number of
(FiboPrime) terms (n) for
Fibonacci prime
series. If n < 3,
program exits with an
error message.
sc Scanner Local (main Facilitates user input
method) reading.
n1 int Local (main Stores the user input
method) for the number of
terms (n) for
Fibonacci prime
series.
i int Local (isprime Counter variable used
method) in the isprime
method to check
divisibility of n.
num int Constructor Parameter for
(FiboPrime) constructor, initializes
n in FiboPrime class.
s int Local (display Stores the Fibonacci
method) number for
processing.
p int Local (display Stores the last digit of
method) s for checking if it's
prime.
Method Listing Table:

Method Prototype Purpose

FiboPrime(int FiboPrime(int num) Constructor: Initializes n to num.


num) If num is less than 3, prints
"INVALID OUTPUT!" and
terminates the program.
display() void display() Displays Fibonacci prime
numbers up to the nth term (n).
Calls fibo(int n) to compute
Fibonacci numbers and
isprime(int n, int i) to check if
the last digit of each Fibonacci
number is prime.
fibo(int n) int fibo(int n) Computes the nth Fibonacci
number recursively using the
Fibonacci sequence formula.

isprime(int n, boolean isprime(int n, Checks if a given number n is


int i) int i) prime recursively by testing
divisibility up to the square root
of n.
main(String public static void Entry point of the program.
args[]) main(String args[]) Reads user input for the nth
term (n1) and initializes an
instance of FiboPrime with n1.
Calls display() to print Fibonacci
prime numbers.
Class Listing Table:

Class Description
FiboPrime Contains methods to find Fibonacci prime numbers up to
nth term
OUTPUT:

You might also like