Java program to print a Fibonacci series


 The Fibonacci series is one of the basic and fundamental number series in mathematics.

This series is obtained by adding the previous two terms of the series.



The basic idea of this series was given by Leonardo of Pisa later known as Fibonacci in his famous book

Liber Abaci in 1202 in the form of a problem known as Leonard’s rabbit problem.

The java program to represent this series is given here:

program using a while loop:

package com.fibonacci;

public class Fibonacci {
public static void main(String[] args) {
int a = 0,b = 1,result = 0;
System.out.println("The fibonacci series is : ");
System.out.println(a);
while(result < 30) {
result = a + b;
a = b;
b = result;
System.out.println(+ result);
}
}
}

Program using a for loop:

public class ForFibonacci {
public static void main(String[] args) {
int a = 0, b = 1, result,i ,n = 10;
System.out.println("The first " +n+" terms of the fibonacci series are: ");
System.out.println(a);
for (i = 0;i<n;i++){
result = a + b;
a = b;
b = result;
System.out.println(+result);
}
}
}







1 comment:

  1. Nice thought! such a informative things you are sharing ,I really liked your content. If you wanna know about "Gurukripa Career Institute | Top NEET Coaching in Sikar and Alwar" go to Best NEET Coaching in Sikar

    ReplyDelete