logic For Fibonacci
fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
fib(10) = 55
import java.io.*;
class Fibonacci {
public static void main(String args[]) {
System.out.println("How many numbers of the sequence would you like?");
InputStreamReader sr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(sr);
try {
String input = br.readLine();
int n = Integer.valueOf(input).intValue();
fibonacci(n);
} catch (NumberFormatException e){
System.out.println("That is no integer. Please enter an integer value only");
} catch (IOException e) {
System.out.println("I did not recieve an input");
}
}
public static void fibonacci(int n){
int a=0,b=1;
for (int i=0;i<n;i++){
System.out.println(a);
a=a+b;
b=a-b;
}
}
}
No comments:
Post a Comment