Welcome To The World Of Java

This site will teach you the basics of Java. It is not necessary to have any prior programming experience.

Java program to print fibonacci series.

Somdeb Burman | 5:53:00 AM | 0 comments

A fibonacci series is one in which the next term is the sum of previous two terms
Example. 0 1 1 2 3 5 8……



import java.util.Scanner;
public class Fibonacci 
{
 public static void main(String args[]) {
  func(10);
 }
 static void func(int n) {
  int a = 0, b = 1, c = 0, i;
  System.out.print(a + " , " + b);
  for (i = 3; i <= n; i++) {
   c = a + b;
   System.out.print(", " + c);
   a = b;
   b = c;
  }
 }
}

Output:

0 , 1, 1, 2, 3, 5, 8, 13, 21, 34
BUILD SUCCESSFUL (total time: 0 seconds)

Java Resources are available for Download from below

Category: , , , ,

0 comments