In this Java program, I show you how to calculate the Fibonacci series of a given number using a recursive algorithm where the fibonacci() method calls itself to do the calculation.
Fibonacci series is a sequence of values such that each number is the sum of the two preceding ones, starting from 0 and 1. The beginning of the sequence is thus:
```
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...
```
Fibonacci series is a sequence of values such that each number is the sum of the two preceding ones, starting from 0 and 1. The beginning of the sequence is thus:
```
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 ...
```
Fibonacci Series Java Program Using Recursion
Java Fibonacci series program using a recursive algorithm where the fibonacci() method calls itself to do the calculation.
/** **/packagenet.javaguides.corejava.programs;importjava.math.BigInteger;importjava.util.Scanner;/** * The program calculates the fibonacci sequence of given number. *@author Ramesh Fadatare **/publicclassFibonacciRecursiveProgram {publicstaticBigIntegerfibonacci(intn) {if (n<=1)returnBigInteger.valueOf(n);return fibonacci(n-2).add(fibonacci(n-1)); }publicstaticvoidmain(String[]args) {Scanner scanner=newScanner(System.in);System.out.print("Enter a number:");int n= scanner.nextInt();for (int i=0; i< n; i++) {BigInteger value= fibonacci(i);System.out.print( value+"\t"); } scanner.close(); }}
Output:
Enter a number: 100 1 1 2 3 5 8 13 21 34Related Java Programs
- Java program to calculate the area of Triangle
- Java Program to Calculate Area of Square
- Java Program to Calculate Area of Rectangle
- Java Program to find the Smallest of three numbers using Ternary Operator
- Java Program to Find Largest of Three Numbers
- Java Program to Find GCD of Two Numbers
- Java Program to Check Armstrong Number
- Java Program to Generate Random Number
- Java Program to Check if Number is Positive or Negative
- Java program to check prime number
- Java Program to Calculate Simple Interest
- Java Program to Swap Two Numbers Without using a Temporary Variable
- Java Program to Swap Two Numbers
- Java Program to Find ASCII Value of a Character
- Java Program to Check Whether an Alphabet is Vowel or Consonant
- Java Program to Check Leap Year
- Java Program to Multiply Two Numbers
- Java Program to Check Even or Odd Number
- Java Program to Add Two Numbers
- Java Program to Swap Two Strings Without Using Third Variable
- Java Program to Swap Two Strings with Third Variable
- How to Get All Digits from String in Java
- Find Duplicate Number in Array in Java
- How to Get Current Working Directory in Java?
- Check Palindrome String in Java
- Java Program to Create Pyramid Of Numbers
Comments
Post a Comment