Table of Contents
Key Applications
Fibonacci Series Program (Non recursive program)
Program Algorithm
1. Fibonacci Series Program (Using Arrays)
2. Fibonacci Series Program (Without Implying Any Loops)
3. Fibonacci Series Program (Without implying Any loops but Achieved only using Conditions)
4. Fibonacci Series Program( Without Loops, the Concepts of Looping is Achieved using Nextint Method)
Conclusion – Fibonacci Series in Java
Home Java javaTutorial Fibonacci Series in Java

Fibonacci Series in Java

Aug 30, 2024 pm 04:25 PM
java

Fibonacci series lies in the process that each number acts to be a sum of two preceding values and the sequence always starts with the base integers 0 and 1. Fibonacci numbers are muscularly related to the golden ratio. In this topic, we are going to see about the Fibonacci Series in Java.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Formula:

an = an − 2 + an − 1
Fibonacci series for first 21 numbers
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
Fibonacci series for first 21 numbers
F0 F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 F14 F15 F16 F17 F18 F19 F20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Key Applications

Here are the Key applications of the Fibonacci Series in Java given below:

  • Miles to kilometer and kilometer to miles conversion.
  • Some instances of Agile methodology.
  • Euclid’s algorithm run time analysis computation is carried out using this series technique.
  • Fibonacci statistics are worn mathematically by some pseudorandom number generators.
  • The poker planning process involves the use of this technique.
  • The data structure technique of the Fibonacci heap is achieved using the Fibonacci series technique.
  • In optics, whilst a shaft of light gleam at a point of view from beginning to end of two piled translucent plates of dissimilar materials of dissimilar refractive indexes, it may return off three surfaces: the pinnacle, center, and base surfaces of the two plates. The numeral of dissimilar ray path to have kreflections, for k > 1, is the {display style k} the Fibonacci number.

Fibonacci Series Program (Non recursive program)

Following is a Fibonacci series program:

Code:

// Fibonacci series program
public class Fibonacci {
// main program
public static void main(String[] args) {
int count = 10, var1 = 0, var2 = 1;
System.out.print("First " + count + " terms: ");
// Fibonacci series formation loop
for (int i = 1; i <= count; ++i)
{
System.out.print(var1 + " + ");
int added_sum= var1 + var2;
var1 = var2;
var2 = added_sum;
}
}
}
Copy after login

Output:

Fibonacci Series in Java

Explanation:

  • This program calculates the Fibonacci series for a given range of numbers.
  • Here this process is achieved using no recursive technique.

Program Algorithm

  • A root class Fibonacci is declared with the necessity that all program codes embedded into this class will address the functionality of achieving a Fibonacci series of numbers.
  • Inside the root class, the main method is declared. The main method acts, as a rule, as a significant Java method. The JVM execution will not take place without the presence of the main method in the program. The explanation of various sub-components of the main method is expressed below,
  • Next, the variable initialization section is implied. This section involves the initialization of three different variables. Two among them are for achieving the Fibonacci logic through a variable level value swap, and another variable is applied for regulating the count of values for which the Fibonacci logic needs to be generated.
  • The Fibonacci series program’s key logic is achieved using the below given for loop in the program section.

Code:

for (int i = 1; i <= count; ++i)
{
System.out.print(var1 + " + ");
int added_sum= var1 + var2;
var1 = var2;
var2 = added_sum;
}
Copy after login
  • The logic behind this for the loop section is as follows; initially, a range of values is being performed on a loop; the loop happens with an increment on the range value for every flow. Additionally, in every flow, the two swap variables’ value is summed up into a third variable.
  • After summing up, the second variable value is implied into the first variable, so this makes the first variable value to be flushed away from this process. In the next step, the summed value is assignated to the second variable.

So at the end of this instance, for a single logic flow, the below happenings are applied:

  • The value of the first variable is flushed away.
  • The existing second variable value is filled in the first variable.
  • The summed up value is moved in into the second variable.

In the process of performing the below logic sequence for the given count of values the need, the Fibonacci series can be achieved.

1. Fibonacci Series Program (Using Arrays)

Code:

import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int Count = 15;
long[] array = new long[Count];
array[0] = 0;
array[1] = 1;
for (int x = 2; x < Count; x++) {
array[x] = array[x - 1] + array[x - 2];
}
System.out.print(Arrays.toString(array));
}
}
Copy after login

Output :

Fibonacci Series in Java

Explanation:

  •  Implying the program logic drafted above, but at this instance, the Fibonacci inputs are stored as part of arrays.
  • So all operations mentioned above are carried out concerning an array.

2. Fibonacci Series Program (Without Implying Any Loops)

Code:

public class Fibonaccifunction
{
private static int indexvalue = 0;
private static int endPoint = 9;
public static void main (String[] args)
{
int number1 = 0;
int number2 = 1;
fibonaccifunction(number1, number2);
}
public static void fibonaccifunction(int number1, int number2)
{
System.out.println("index value : " + indexvalue + " -> " + number1);
if (indexvalue == endPoint)
return;
indexvalue++;
fibonaccifunction(number2, number1+number2);
}
}
Copy after login

Output :

Fibonacci Series in Java

Explanation:

  •  Implying the program logic drafted above but at this instance, the Fibonacci inputs handled recursively using a function named Fibonacci.

3. Fibonacci Series Program (Without implying Any loops but Achieved only using Conditions)

Code:

public class Fibonacci_with_conditions
{
static int number2=1;
static int number1=0;
static int next=0;
public static  void Fibonacci_conditions( int number)
{
if(number<10)
{
if(number == 0)
{
System.out.print(" "+number);
number++;
Fibonacci_conditions (number);
}
else
if(number == 1)
{
System.out.print(" "+number);
number++;
Fibonacci_conditions(number);
}
else{
next=number1+number2;
System.out.print(" "+next);
number1=number2;
number2=next;
number++;
Fibonacci_conditions(number);
}
}
}
public static void main(String[] args)
{
Fibonacci_conditions(0);
}
}
Copy after login

Output:

Fibonacci Series in Java

Explanation:

  • Implying the program logic drafted above, but at this instance, the Fibonacci inputs are regulated only through necessary conditional statements.
  • According to the conditions, the swapping of the variables is necessarily carried out.

4. Fibonacci Series Program( Without Loops, the Concepts of Looping is Achieved using Nextint Method)

Code:

import java.util.*;
public class Fibonacci_series
{
public static void main(String[] args)
{
System.out.println("Input:");
int number= 10,value1=1,value2=0,value3=0;
num(number, value1, value2, value3);
}
public static void num(int number,int value1,int value2,int value3)
{
if(value1 <= number)
{
System.out.println(value1);
value3=value2;
value2=value1;
value1=value2+value3;
num(number,value1,value2,value3);
}
}
}
Copy after login

Output:

Fibonacci Series in Java

Explanation:

  • Implying the program logic drafted above, but at this instance, the Fibonacci inputs are handled recursively using a function named num, and the loop is carried out using the nextInt function.

Conclusion – Fibonacci Series in Java

These programs are implied to achieve the Fibonacci series for a given integer value.  A largely classified set of techniques are implied in the given list of examples. Techniques like an array-oriented approach and a condition-alone approach are very much peculiar.

The above is the detailed content of Fibonacci Series in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24
Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP's Impact: Web Development and Beyond PHP's Impact: Web Development and Beyond Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP vs. Python: Use Cases and Applications PHP vs. Python: Use Cases and Applications Apr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles