Header Ads

Java Program to Print Fibonacci Series

A Fibonacci Series is The Integer Sequence of 0, 1, 1, 2, 3, 5, 8….
The first two Number are 0 and 1. 
All Other numbers are obtained by Adding the Preceding Two Number.

Java, Program, Print, Fibonacci, Series, find Fibonacci series, java program, java program find Fibonacci series

Example: Code Program


import java.util.*;
public class Fibonacci {

     public static void main(String[] args) {
          // TODO Auto-generated method stub
          Scanner sc = new Scanner(System.in);
          int n1 = 0, n2 = 1, num, count, next;
          System.out.print("How Many Fibonacci Terms Required:");
          num = sc.nextInt();
          System.out.print(n1+ " " +n2);
          count = 2;
          while(count < num) {
             
              next = n1+n2;
              System.out.print(" "+next+" ");
              count++;
              n1 = n2;
              n2 = next;
          }
         

     }

}
Output

Java, Program, Print, Fibonacci, Series, find Fibonacci series, java program, java program find Fibonacci series

No comments

Powered by Blogger.