The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Program to Display Fibonacci Series
In this article we will se different approaches to display Fibonacci series. Before going to the actual concept let’s first understand what is this Fibonacci series.
Fibonacci series : It represents a series of numbers where the next number is the sum of previous two numbers.
For example: Fibonacci series upto 10 terms 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
Logic:
next=one+two; one=two; two=next;
Let’s see one by one:
Method-1 : Fibonacci Series using for loop
By using for loop we can get the Fibonacci series.
Approach:
- Take the first two terms of Fibonacci series as 0 and 1 and assign them to two integer variables say ‘
one
‘, ‘two
‘ respectively. - Then take a third variable say ‘
count
‘, to represent numbers of terms in Fibonacci series. - Then take a for loop to get the series upto mentioned term by using Fibonacci logic.
- Go on printing one by one series number in the for loop.
See the below program to understand it clearly.
class Fibo { public static void main(String[] args) { int one = 0, two = 1, count=10; System.out.println("Fibonacci Series upto " + count + " terms:"); // for loop to get fibonacci till 10 terms for (int i = 1; i <= count; i++) { System.out.print(one + " "); // computing the next term int next = one+two; one = two; two = next; } } }
Output: Fibonacci Series upto 10 terms: 0 1 1 2 3 5 8 13 21 34
Method-2 : Fibonacci Series using while loop
By using while loop we can get the Fibonacci series.
Approach:
- Approach is completely same as for loop, only in this we will loop through while instead of for.
See the below program to understand it clearly.
class Fibo { public static void main(String[] args) { int one = 0, two = 1, count=10; System.out.println("Fibonacci Series upto " + count + " terms:"); // i value initialized int i=1; // while loop to get fibonacci till 10 terms while(i<=count) { System.out.print(one + " "); // computing the next term int next = one+two; one = two; two = next; //incrementing i i++; } } }
Output: Fibonacci Series upto 10 terms: 0 1 1 2 3 5 8 13 21 34
Method-3 : Fibonacci Series using recursion
By using recursion also we can get the Fibonacci series.
Approach:
- Take a user defined method say
Fibonacci()
method. - Then put Fibonacci logic inside it.
- Then call that
Fibonacci()
method repeatedly according to the Fibonacci terms say ‘count
‘ value.
See the below program to understand it clearly.
class Fibo { // one and two value initialized static int one=0,two=1,next=0; // user defined method static void Fibonacci(int count) { // will be executed till count value is greater than 0 // means inside this Fibonacci() method will be called recursively 10 times. if(count>0) { System.out.print(one+" "); int next = one + two; one = two; two = next; // Fibonacci() method calling itself i.e. recursion Fibonacci(count-1); } } // main method public static void main(String args[]) { // count value initialized int count=10; // prinitng the fibonacci series System.out.println("Fibonacci Series upto " + count + " terms:"); // calling the Fibonacci() method Fibonacci(count); } }
Output: Fibonacci Series upto 10 terms: 0 1 1 2 3 5 8 13 21 34
Related Basic Java Programs:
- Java Program to Print an Integer (Entered by the User)
- Java Program to Add Two Integers
- Java Program to Multiply two Floating Point Numbers
- Java Program to Find ASCII Value of a character
- Java Program to Compute Quotient and Remainder
- Java Program to Swap Two Numbers
- Java Program to Check Whether a Number is Even or Odd
- Java Program to Find the Largest Among Three Numbers
- Java Program to Find the Frequency of Character in a String
- Java Program to Remove All Whitespaces from a String
- Java Program to Round a Number to n Decimal Places
- Java Program to Check if a String is Empty or Null