Monday, 14 April 2014

JAVA Program To Implement The Fibonacci Series


Write a JAVA Program To Implement The Fibonacci Series

Ex: FIBONACCI SERIES

Aim:To write a JAVA program to implement the Fibonacci series

Algorithm:

Step 1: Start the program.
Step 2: Declare n=10. So find the Fibonacci series up to 10.
Step 3: Initialize a=-1, b=1.
Step 4: Add a and b value and store it in int.
Step 5: Display the f values.
Step 6: Then assign a=b and b=f.
Step 7: For I from 1 to 10 repeat the step 4, 5 and 6.
Step 8: Stop the program.


Program:

import java.io.*;
classfibo
{
public static void main(String args[])
{
int a=-1, b=1, i, f, n=10;
System.out.println("Fibonacci series is\n");
for (i=1; i<=n; i++)
{
f=a+b;
System.out.println(f);
a=b;
b=f;
}
}
}


Output:
0
1
1
2
3
5
8
13
21
34

Result:

Thus the implementation of JAVA program for the Fibonacci series is executed and the output has been verified.

0 comments:

Post a Comment