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

Fibonacci Cal

Uploaded by

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

Fibonacci Cal

Uploaded by

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

Import javax.swing.

JOptionPane;

Import java.math.BigInteger;

Public class FibonacciCalculator {

Public static BigInteger fibonacciRecursive(int n) {

If (n <= 1) {

Return BigInteger.valueOf(n);

Return fibonacciRecursive(n – 1).add(fibonacciRecursive(n – 2));

Public static String fibonacciRecursiveSequence(int length) {

StringBuilder result = new StringBuilder(“Fibonacci Sequence


(Recursive):\n”);

For (int I = 0; I < length; i++) {

Result.append(fibonacciRecursive(i)).append(“ “);

Return result.toString();

Public static void main(String[] args) {

Boolean continueProgram = true;

Boolean programCanceled = false;

While (continueProgram) {
String input = JOptionPane.showInputDialog(null, “Enter the desired
length of the Fibonacci sequence (or press Cancel to exit):”);

If (input == null) {

programCanceled = true;

break;

Try {

Int length = Integer.parseInt(input);

If (length < 1) {

JOptionPane.showMessageDialog(null, “Please enter a positive


integer.”);

Continue;

String[] options = {“Recursive”};

Int choice = JOptionPane.showOptionDialog(

Null,

“Choose the calculation method:”,

“Fibonacci Calculation Method”,

JOptionPane.DEFAULT_OPTION,

JOptionPane.INFORMATION_MESSAGE,

Null,

Options,

Options[0]
);

String result;

If (choice == 0) {

Result = fibonacciRecursiveSequence(length);

} else {

JOptionPane.showMessageDialog(null, “No valid option


selected.”);

Continue;

JOptionPane.showMessageDialog(null, result);

Int continueChoice = JOptionPane.showOptionDialog(

Null,

“Do you want to continue?”,

“Continue or Exit”,

JOptionPane.YES_NO_OPTION,

JOptionPane.INFORMATION_MESSAGE,

Null,

New Object[] {“Yes”, “No”},

“Yes”

);

If (continueChoice == JOptionPane.NO_OPTION) {

continueProgram = false;

}
} catch (NumberFormatException e) {

JOptionPane.showMessageDialog(null, “Invalid input. Please enter a


numeric value.”);

If (!programCanceled) {

JOptionPane.showMessageDialog(null, “Thank you for using the


Fibonacci Calculator. Goodbye!”);

You might also like