fibonacci sequence java
Java
//Fibonacci Series using Recursion
#include<bits/stdc++.h>
using namespace std;
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main ()
{
int n = 9;
cout << fib(n);
getchar();
return 0;
}
// This code is contributed
// by Akanksha Rai
/*
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.");
}
}# Function for nth Fibonacci number
def Fibonacci(n):
if n<0:
print("Incorrect input")
# First Fibonacci number is 0
elif n==0:
return 0
# Second Fibonacci number is 1
elif n==1:
return 1
else:
return Fibonacci(n-1)+Fibonacci(n-2)
# Driver Program
print(Fibonacci(9))
#This code is contributed by Saket Modi
//Fibonacci Series using Recursion
#include<stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
int main ()
{
int n = 9;
printf("%d", fib(n));
getchar();
return 0;
}
// C# program for Fibonacci Series
// using Recursion
using System;
public class GFG
{
public static int Fib(int n)
{
if (n <= 1)
{
return n;
}
else
{
return Fib(n - 1) + Fib(n - 2);
}
}
// driver code
public static void Main(string[] args)
{
int n = 9;
Console.Write(Fib(n));
}
}
// This code is contributed by Sam007
Also in Java:
- Title
- android dismiss keyboard
- Category
- Java
- Title
- how to remove all whitespace from string java
- Category
- Java
- Title
- how to open a folder in java swing project
- Category
- Java
- Title
- mock ParameterizedTypeReference
- Category
- Java
- Title
- spigot cancel repeating task
- Category
- Java
- Title
- java console text color
- Category
- Java
- Title
- index of an array procesing
- Category
- Java
- Title
- how to check if in array java
- Category
- Java
- Title
- android notification addaction example
- Category
- Java
- Title
- test function that call a function javascrip
- Category
- Java
- Title
- java check if directory exists
- Category
- Java
- Title
- iterate map in java 8 using stream
- Category
- Java
- Title
- javadoc reference general toString
- Category
- Java
- Title
- cannot lock java compile cache as it has already been locked by this process
- Category
- Java
- Title
- checking if a given integer is a prime number java
- Category
- Java
- Title
- java cmd install raspbian
- Category
- Java
- Title
- replaceall in java
- Category
- Java
- Title
- spigot spawn firework
- Category
- Java
- Title
- arrays.fill java
- Category
- Java
- Title
- java write arraylist of objects to file
- Category
- Java
- Title
- get first 5 characters of string java
- Category
- Java
- Title
- covariant type in java
- Category
- Java
- Title
- java define main function
- Category
- Java
- Title
- java pause program until key pressed
- Category
- Java
- Title
- instantiate optinal java 8
- Category
- Java
- Title
- how to get individual words from a string in java
- Category
- Java
- Title
- how to create array of linkedlist in java
- Category
- Java
- Title
- como detener un void java
- Category
- Java
- Title
- how to make a variable unchangeable in java
- Category
- Java
- Title
- string isEmpty java
- Category
- Java
- Title
- junit meaning in java
- Category
- Java
- Title
- java Convert a string IPv4 IP address to the equivalent long numeric value.
- Category
- Java
- Title
- How to loop through objects in java using streams
- Category
- Java
- Title
- money value commas java
- Category
- Java
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- are strings modifiable
- Category
- Java
- Title
- fragment manager in android
- Category
- Java
- Title
- java convert edittext to double
- Category
- Java
- Title
- Common GIt Commands
- Category
- Java
- Title
- linux pyspark select java version
- Category
- Java
- Title
- Java Read Files
- Category
- Java
- Title
- how to check internet is working or not in java
- Category
- Java
- Title
- java code to get all leaf nodes of a xml
- Category
- Java
- Title
- java switch
- Category
- Java
- Title
- how to end a program in an if statement java
- Category
- Java
- Title
- Filebody in java
- Category
- Java
- Title
- how to remove all special characters from a string in java
- Category
- Java
- Title
- java builder pattern example
- Category
- Java
- Title
- java how to show an array
- Category
- Java
- Title
- reentrantreadwritelock java
- Category
- Java
- Title
- system.in example in java
- Category
- Java
- Title
- how to set a windows background image tkinter
- Category
- Java
- Title
- tolowercase java
- Category
- Java
- Title
- java insertion sort
- Category
- Java
- Title
- leap year program in java
- Category
- Java
- Title
- how to create gravity 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
- arraylist set method
- Category
- Java
- Title
- java string to integer
- Category
- Java
- Title
- sum of arraylist java 8
- Category
- Java
- Title
- how to use for loop for array in java
- Category
- Java
- Title
- javafx polygon
- Category
- Java
- Title
- sdkmanager JAVA_HOME invalid directory
- Category
- Java
- Title
- java how to define a function
- Category
- Java
- Title
- how do I change the opacity of a JButton
- Category
- Java
- Title
- bootstrap button bigger
- Category
- Java
- Title
- how to compare strings java
- Category
- Java
- Title
- arraylist array
- Category
- Java
- Title
- java thread
- Category
- Java
- Title
- how to init an array with objects in java
- Category
- Java
- Title
- how to copy all files and subdirectories in directory in java
- Category
- Java
- Title
- foreach java
- Category
- Java
- Title
- java strin to int
- Category
- Java
- Title
- how to get length of integer in java
- Category
- Java
- Title
- java map example
- Category
- Java
- Title
- java scanner next()
- Category
- Java
- Title
- string to char in java
- Category
- Java
- Title
- java string to int
- Category
- Java
- Title
- java quit application
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- java for in loop
- Category
- Java
- Title
- list java oracle
- Category
- Java
- Title
- java how to get all threads
- Category
- Java
- Title
- switch en java
- Category
- Java
- Title
- java fullscreen jframe
- Category
- Java
- Title
- java function that returns the index of the largest value in an array
- Category
- Java
- Title
- java stack push
- Category
- Java
- Title
- java get int from double without rounding
- Category
- Java
- Title
- jbutton remove selection border
- Category
- Java
- Title
- compare list from db and list from request
- Category
- Java
- Title
- how to push an element in hashset java
- Category
- Java
- Title
- how to remove all components from layeredPane java
- Category
- Java
- Title
- how to remove duplicates from an array java
- Category
- Java
- Title
- how to count an replace string in java
- Category
- Java
- Title
- how to check if the file has remaining without reading from it java
- Category
- Java
- Title
- java list to array
- Category
- Java
- Title
- array in java
- Category
- Java
- Title
- java substring
- Category
- Java
- Title
- get day name from date in java
- Category
- Java
- Title
- how to install java 8 on terminal os
- Category
- Java