In this tutorial we are going to learn how to print Fibonacci series in Java program using iterative method.
In this series number of elements of the series is depends upon the input of users. Program will print n number of elements in a series which is given by the user as a input.
Read this: What is Fibonacci series? Fibonacci series program using iteration in c.
How this java program will work?
This Java program will take a integer as an input. This input is a number upto which series will print.
Suppose if someone is given input as 5 then output will be
0, 1, 1, 2, 3
Program 1: Fibonacci Series Program in Java using Iterative methods
import java.util.*;
class Main{
public static void main(String ...a){
int first = 0, second = 1, result, i;
Scanner sc= new Scanner(System.in);
System.out.print("Enter number- ");
int n= sc.nextInt();
System.out.println("fibonacci series is: ");
for (i = 0; i < n; i++)
{
if (i <= 1)
result = i;
else
{
result = first + second;
first = second;
second = result;
}
System.out.println(result);
}
}
}
Output:
Enter number- 5
fibonacci series is:
0
1
1
2
3
Conclusion:
In this tutorial, we have explored writing Fibonacci series program using iterative method in Java. This will generate and print the Fibonacci series. The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones, usually starting with 0 and 1.
Key Points:
- User Input:
- Our above program allows users to input an integer
n
to determine the number of elements they want in the Fibonacci series.
- Our above program allows users to input an integer
- Iterative Method:
- Above Java program uses an iterative approach to generate the Fibonacci series.
- It initializes the first two elements (
first
andsecond
) to 0 and 1, respectively. - The program then uses a
for
loop to calculate and print each subsequent element in the series.
- Logic in Loop:
- Within the loop, the logic distinguishes between the first two elements and calculates subsequent elements based on the sum of the previous two.
- The loop continues for
n
iterations, printing each element of the Fibonacci series.
- Output Illustration:
- The output, illustrated with an example where
n
is set to 5, demonstrates the generation of the Fibonacci series: 0, 1, 1, 2, 3.
- The output, illustrated with an example where
Happy coding! 🚀
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…