Training Outcomes Within Your Budget!

We ensure quality, budget-alignment, and timely delivery by our expert instructors.

Share this Resource

Table of Contents

Fibonacci Series in Java
The Fibonacci Series is a number series where each consecutive number is the sum of the previous two numbers. The Fibonacci Series in Java always begins with 0 and 1, which are added to produce the third number, which is then added to the second, and this mathematical operation reiterates infinitely. 

The Fibonacci Series is significantly used for determining the greatest common divisor of two integers. This series is a sequence where the number is the sum of the previous two numbers. In this blog, you'll learn how to display the Fibonacci Series in Java.

Table of Contents

1) Understanding the Fibonacci Series in Java

2) Implementing the Fibonacci Series in Java

   a) Using For Loop

   b) Using While Loop

   c) Using the user input

   d) Using Recursion

3) Conclusion

Understanding the Fibonacci Series in Java

In Fibonacci Series, two numbers are added to produce a third number, which is then added to the second, and this mathematical operation reiterates itself infinitely. It is a number series where each consecutive number is the sum of the previous two numbers.

The Fibonacci Series: 0,1,1,2,3,5,8,13,21, etc.

0 and 1 are the first two values to begin the arithmetic operation of addition, reiterated and proceeds to infinity.

Java Certification
 

Implementing the Fibonacci Series in Java

There are different ways to implement the Fibonacci Series in Java, such as using For Loop, While Loop, User Input, or Recursion. Here is a brief explanation of each method:

Using  For Loop

This method uses a For Loop to iterate from 1 to n, where n is the number of terms to be printed. In each iteration, the next term is calculated by adding the previous two terms, and the values of the previous two terms are updated accordingly. This method has a time complexity of:

                                                                       O(n)

And a space complexity of:

                                                                      O(1)

Here is an example code using For Loop:
 

Implementing the Fibonacci Series in Java

Using the For Loop

class Main
{ public static void main(String[] args)
{
int n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i)
{
System.out.print(firstTerm + ", ");
// compute the next term
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}

Using the While Loop

This method uses a While Loop to iterate until a certain condition is met, such as the number of terms printed or the value of the current term. In each iteration, the next term is calculated by adding the previous two terms, and the values of the previous two terms are updated accordingly. This method has the same time and space complexity as the fFor Loop method. Here is an example code using While Loop:

//Using the While Loop
class Main
{
public static void main(String[] args)
{
int i = 1, n = 10, firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
while (i <= n)
{
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
i++;
}
}
}

Unlock the world of web development: Join our JavaScript for Beginners Course today!

Using User Input

This method uses the Scanner class to get the input from the user, such as the number of terms to be printed or the limit of the series. The input is then used to control the loop condition, and the rest of the logic is similar to the previous methods. Here is an example code using User Input:

import java.util.*;
public class THKFibbonaci
{
public static void main(String[] args)
{
//Syntax to take input value from user
//Declare the Scanner class instance
Scanner sc=new Scanner(System.in);
int t1 = 0, t2 = 1;
System.out.print("Number of terms to be printed here: ");
int n=sc.nextInt();
//Here you you will declare how many terms yournseries will have
System.out.println("The First " + n + " terms of fibonnaci series: ");
//Print statement to get your Fibbonaci series
for (int i = 1; i <= n; ++i)
{ System.out.print(t1 + " ");
int total = t1 + t2;
t1 = t2;
t2 = total;
}
}
}


Enhance your knowledge of Java and learn to build your website and applications. Sign up for the Java Programming course now!

Using Recursion

This method uses a Recursive Function to calculate the nth term of the Fibonacci series, where n is the input parameter. The Recursive Function calls itself with smaller values of n until it reaches the base case, which is n=0 or n=1, and then returns the value of n. The Recursive Function has a time complexity of: 

                                                     O(2n)

And a space complexity of: 

                                                    O(n)

Which are much higher than the Loop methods. Here is an example code using Recursion:
 

public class Fibonacci
{
public static void main(String[] args)
{
int n = 10; // number of terms to generate
System.out.print("Fibonacci series up to " + n + " terms: ");
for (int i = 0; i < n; i++)
{
System.out.print(fibonacci(i) + " ");
}
}
public static int fibonacci(int n)
{
if (n == 0)
{
return 0;
} else if (n == 1)
{
return 1;
}
else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
}


Master the art of programming: Start your journey with our comprehensive Java Courses now!

Conclusion

The Fibonacci Series in Java is a sequence extending to infinity, using a mathematical operation reiterating the addition of every two consecutive numbers. This blog has introduced you to the different Java programming logical structures which generate the Fibonacci Series in Java. These techniques include the For Loop, the While Loop, User input-based and Recursion.

Design your own corporate web applications and high-load services with good reliability. Sign up for the Web Development using Java Training course now!

Frequently Asked Questions

What is the method of implementing Fibonacci Series in Java? faq-arrow

There are different methods to implement the Fibonacci Series in Java, such as using For Loop, While Loop, Recursion, memoisation, or stream API. Each method has its own advantages and disadvantages in terms of time and space complexity, readability, and scalability.

How can I optimise the Fibonacci sequence calculation in Java for large numbers? faq-arrow

For large numbers, use the iterative approach or dynamic programming. Both avoid the excessive Recursion overhead and stack overflow issues, providing faster and more efficient computation for large indices. hods mentioned above, depending on the requirements and constraints of the problem.

What are the other resources and offers provided by The Knowledge Academy? faq-arrow

The Knowledge Academy takes global learning to new heights, offering over 30,000 online courses across 490+ locations in 220 countries. This expansive reach ensures accessibility and convenience for learners worldwide.  

Alongside our diverse Online Course Catalogue, encompassing 17 major categories, we go the extra mile by providing a plethora of free educational Online Resources like News updates, Blogs, videos, webinars, and interview questions. Tailoring learning experiences further, professionals can maximise value with customisable Course Bundles of TKA.
 

What is Knowledge Pass, and how does it work? faq-arrow

The Knowledge Academy’s Knowledge Pass, a prepaid voucher, adds another layer of flexibility, allowing course bookings over a 12-month period. Join us on a journey where education knows no bounds.

What are related courses and blogs provided by The Knowledge Academy? faq-arrow

The Knowledge Academy offers various Java Courses, including Java Programming, Hibernate Training and Introduction to Java EE Course. These courses cater to different skill levels, providing comprehensive insights into Latest Java Technologies Trends.

Our Programming and DevOps blogs cover a range of topics related to Java Programming, offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your Java Programming skills, The Knowledge Academy's diverse courses and informative blogs have you covered.
 

Upcoming Programming & DevOps Resources Batches & Dates

Date

building Java Programming

Get A Quote

WHO WILL BE FUNDING THE COURSE?

cross

OUR BIGGEST SPRING SALE!

Special Discounts

red-starWHO WILL BE FUNDING THE COURSE?

close

close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.

close

close

Press esc to close

close close

Back to course information

Thank you for your enquiry!

One of our training experts will be in touch shortly to go overy your training requirements.

close close

Thank you for your enquiry!

One of our training experts will be in touch shortly to go over your training requirements.