Recursive implementation of the Fiboacci number

2019-09-30

import  java.util.ArrayList;

import java.util.List;
import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {

/**
* 11~23, get the nth Fibonacci number
*/
List
list = new ArrayList();

Scanner sc
= new Scanner(System.in);

System.out.println(
"How many Fibonacci numbers do you want to get:");

int n = sc.nextInt();

for (int i = 0; i ) {

list.add(me(i));
}
System.out.println("the "+n+" Fibonacci number is: "+list.get(n-1) );

/**
* 28~31, get the first 10 Fibonacci numbers
*//*
for (int i=0;i<10;i++) {

System.out.println(me(i));
}
*/
}

static int me(int n) {

if (n <0) {
throw new IllegalArgumentException("x<0"< span style="color: #000000;">);
}
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return me(n-1) + me(n-2) ;
}
}

import java.util.ArrayList;

import java.util.List;
import java.util.Scanner;

public class Demo01 {
public static void main(String[] args) {

/**
* 11~23, get the nth Fibonacci number
*/
List
list = new ArrayList();

Scanner sc
= new Scanner(System.in);

System.out.println(
"How many Fibonacci numbers do you want to get:");

int n = sc.nextInt();

for (int i = 0; i ) {

list.add(me(i));
}
System.out.println("the "+n+" Fibonacci number is: "+list.get(n-1) );

/**
* 28~31, get the first 10 Fibonacci numbers
*//*
for (int i=0;i<10;i++) {

System.out.println(me(i));
}
*/
}

static int me(int n) {

if (n <0) {
throw new IllegalArgumentException("x<0"< span style="color: #000000;">);
}
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
return me(n-1) + me(n-2) ;
}
}

Leave a Comment

Your email address will not be published.