Tuesday, 12 March 2013

Java Program For Generating Fibonacci Sequence


/* Java Program For Generating Fibonacci Sequence Is Defined by The Following Rule:
The first two values in the sequence are 1 & 1. Every subsequent value is the
sum of two values preceding it.*/

// File Name: fibonacci.java

import java.io.*;
class fibonacci {
public static void main(String args[]){
int a=1,b=1,c=a+b,n;
n=Integer.parseInt(args[0]);
                System.out.println("www.shashanktechnologies.blogspot.com");
System.out.println("Given Value For n Is:"+n);
System.out.print("Fibonacci Sequence Generated For "+n+" Is: ");
System.out.print(a+","+b+",");
while(c<=n){
System.out.print(c+",");
a=b;
b=c;
c=a+b;
}

}
}


OUTPUT:

COMPILATION: G:\shashank>javac fibonacci.java

INTERPRETATION: G:\shashank>java fibonacci 10





No comments:

Post a Comment