fibonacci sequence java using recursion
Java
/*
Author: Jeffrey Huang
This finds the fibonacci number using a term provided by a user
A recursive method is used.
b)
9 calls are made to the fibonacci sequence in order to compute the 5th term.
*/
import java.io.*;
public class Fibonacci_JeffreyHuang
{
//static int counter=0;
public static long Fibonacci (long n)
{
//count++;
if (n == 1 || n == 2)
{
return 1;
}
else if (n <= 0)
{
System.out.println ("Error: There is no term less than 1");
return 0;
}
else
{
return (Fibonacci (n - 1) + Fibonacci (n - 2));
}
}
public static void main (String[] args) throws IOException
{
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
String sentinel = "yes";
long fib_term = 0;
boolean invalid;
while (sentinel.equalsIgnoreCase ("yes") || sentinel.equalsIgnoreCase ("y"))
{
//input
System.out.println ("Which fibonacci term would you like to find?");
invalid = true;
while (invalid == true)
{
invalid = false;
try
{
fib_term = Integer.parseInt (br.readLine ());
/*
while (fib_term<=0)
{
System.out.println ("Invalid input. Please try again.");
System.out.println ();
fib_term = Integer.parseInt (br.readLine ());
}
*/
}
catch (Exception e) //in case the string is not a number
{
System.out.println ("Invalid input. Please try again.");
System.out.println ();
invalid = true;
}
}
//output
System.out.println ();
if (fib_term <= 0)
{
Fibonacci (fib_term);
}
else
{
System.out.println ("Term " + fib_term + " of the fibonacci sequence is " + Fibonacci (fib_term));
}
//System.out.println(counter);
//rerun program
System.out.println ();
System.out.println ("Would you like to run the program again? (y/n)");
System.out.println ();
sentinel = br.readLine ();
System.out.println ();
while (!(sentinel.equalsIgnoreCase ("yes") || sentinel.equalsIgnoreCase ("y")
|| sentinel.equalsIgnoreCase ("n") || sentinel.equalsIgnoreCase ("no")))
{
//System.out.println();
System.out.println ("Invalid input. Please try again.");
System.out.println ("Would you like to run the program again? (y/n)");
sentinel = br.readLine ();
System.out.println ();
}
}
System.out.println ("Program terminated by user.");
}
}
Also in Java:
- Title
- replaceall in java
- Category
- Java
- Title
- how to calculate exponential in java
- Category
- Java
- Title
- java loop through map
- Category
- Java
- Title
- java boolean zen
- Category
- Java
- Title
- byte java
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- java get last element of list
- Category
- Java
- Title
- separateur JMenu swing java
- Category
- Java
- Title
- how to create an array of arraylist in java
- Category
- Java
- Title
- java while
- Category
- Java
- Title
- get first character of string java
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- how to fill a 2d array in java
- Category
- Java
- Title
- StringBuilder java pop back
- Category
- Java
- Title
- how to read in a file in java
- Category
- Java
- Title
- random processing
- Category
- Java
- Title
- ceil function in java
- Category
- Java
- Title
- java go troght loop object
- Category
- Java
- Title
- numberformatexception
- Category
- Java
- Title
- java replace all xml decoding
- Category
- Java
- Title
- how to add strings together
- Category
- Java
- Title
- java array declaration
- Category
- Java
- Title
- j
- Category
- Java
- Title
- java 8 map foreach
- Category
- Java
- Title
- java arraylist in enum
- Category
- Java
- Title
- récupérer les données d un liste java
- Category
- Java
- Title
- remove space string java
- Category
- Java
- Title
- java animated gif example
- Category
- Java
- Title
- for loop in java stack overflow
- Category
- Java
- Title
- compile java
- Category
- Java
- Title
- how to draw a line in java
- Category
- Java
- Title
- java random boolean
- Category
- Java
- Title
- android clear specific sharedpreference value
- Category
- Java
- Title
- reverse array in java
- Category
- Java
- Title
- java hahmap
- Category
- Java
- Title
- java best way to concatenate strings
- Category
- Java
- Title
- add one character to string java
- Category
- Java
- Title
- read a mail and its content in java mail api
- Category
- Java
- Title
- programa que convierete un archi de c a java
- Category
- Java
- Title
- 2d arrays | java
- Category
- Java
- Title
- how to take binary input in java
- Category
- Java
- Title
- declare String in java
- Category
- Java
- Title
- Write a JAVA method that expands a given binomial (ax + by)n, where integers a, b, n are user inputs. For example, if a = 2, b = -12, n = 4 are entered the method should print or return
- Category
- Java
- Title
- java stack
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- how to format a double in java to 2 decimal places
- Category
- Java
- Title
- java create file if not exists
- Category
- Java
- Title
- loop while in java
- Category
- Java
- Title
- faire un timer en java
- Category
- Java
- Title
- how to create java jframe in eclipse
- Category
- Java
- Title
- java initialize string array
- Category
- Java
- Title
- list java replace
- Category
- Java
- Title
- stackoverflow java enum with constructor
- Category
- Java
- Title
- java calculator code
- Category
- Java
- Title
- bukkit java get player count
- Category
- Java
- Title
- android studio remove notification bar
- Category
- Java
- Title
- convert array to list java
- Category
- Java
- Title
- how to separate no and text in java
- Category
- Java
- Title
- check if string is null or empty java
- Category
- Java
- Title
- java random between two strings
- Category
- Java
- Title
- how to do 4th root java
- Category
- Java
- Title
- how to make a dictionary in java
- Category
- Java
- Title
- Error executing Maven. java.io.FileNotFoundException: The specified user settings file does not exist: /etc/java-8-openjdk
- Category
- Java
- Title
- how to create an array list in java
- Category
- Java
- Title
- create jdbc connection in java
- Category
- Java
- Title
- abstract class java constructor
- Category
- Java
- Title
- value receive null with post method in the java spring controller
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- Could not resolve project :app
- Category
- Java
- Title
- java hashmap get value
- Category
- Java
- Title
- get tfidf score for a sentence
- Category
- Java
- Title
- what is a variable in java
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- java int to hex fixed length
- Category
- Java
- Title
- java if a or b
- Category
- Java
- Title
- android dismiss keyboard
- Category
- Java
- Title
- how to scan a character in java
- Category
- Java
- Title
- how to stop screen rotation in android code
- Category
- Java
- Title
- java int passed by reference
- Category
- Java
- Title
- how to use for loop for array in java
- Category
- Java
- Title
- java stream order by property
- Category
- Java
- Title
- priorityqueue poll
- Category
- Java
- Title
- java sleep in code
- Category
- Java
- Title
- loop collection android
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- what is a method example in java
- Category
- Java
- Title
- how to get the width and height of a string in java
- Category
- Java
- Title
- java xml element get attribute value
- Category
- Java
- Title
- how to collect objective in java
- Category
- Java
- Title
- java string array
- Category
- Java
- Title
- java for each
- Category
- Java
- Title
- java max
- Category
- Java
- Title
- java override equals and hashcode
- Category
- Java
- Title
- how to create a random number in java
- Category
- Java
- Title
- java check if file exists
- Category
- Java
- Title
- counting repeated characters in a string in java
- Category
- Java
- Title
- trim() and split() in java
- Category
- Java
- Title
- android studio keep device awake
- Category
- Java
- Title
- how to know when user is done typing android
- Category
- Java