English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

While I understand the concept of the Fibonacci sequence (i.e. each number is the sum of the previous two numbers) I am having trouble implementing it in Java.

For instance, if I accept a starting number as input, I'd like the program to compute the next 20 numbers in the series (i.e. if the user enters 10, the output would be 10, 10, 20, 30, 50, 80, 130, etc.)

2007-08-07 09:21:36 · 6 answers · asked by Chris S 2 in Computers & Internet Programming & Design

6 answers

It's as simple as this:

Call this myprog.java :

public class myprog {
public static void main(String args[]){
int a = 0;
int b = Integer.parseInt(args[0]);

for (int i = 1; i <= 20; i++) {
a = a + b;
b = a - b;
System.out.println(a);
}
}
}

First argument is starting number.
For 10, this gives the desired output
10,10,20,30,... 41810 , 67650

try for yourself!

2007-08-07 10:25:15 · answer #1 · answered by E.M.Bed 5 · 0 2

Well agree above post yet think no Exception been thrown...thts why i tried to put more advanced n short CODE

public class Fibonacci {

public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
if (N < 10|| N > 130) {
throw new RuntimeException("N must be between 10 and 130");
}

long[] fib = new long[N+1];

// base cases
fib[0] = 0;
fib[1] = 1;

// bottom-up dynamic programming
for (int n = 2; n <= N; n++)
fib[n] = fib[n-1] + fib[n-2];

// print results
System.out.println(fib[N]);

}
}


hope this helps
Cheers :)

2007-08-07 10:14:42 · answer #2 · answered by Neeraj Yadav♥ 6 · 0 0

Each number is the result of the addition of the proceeding two. This is a very simple algorithm to implement if you understand the basics of loops and arrays. Have you tried?

2016-05-21 01:00:21 · answer #3 · answered by le 3 · 0 0

This uses the command line rather than prompting, but will do what you want:

========================
public class Fibo {
public static void main(String[] args) {
int prevNum = Integer.parseInt(args[0]);
int prevPrevNum = 0;
for (int i=0; i<20; ++i) {
System.out.println("" + prevNum);
int nextNum = prevNum + prevPrevNum;
prevPrevNum = prevNum;
prevNum = nextNum;
} } }
==========================

Sample output:

10
10
20
30
50
80
130
210
340
550
890
1440
2330
3770
6100
9870
15970
25840
41810
67650

2007-08-07 09:27:17 · answer #4 · answered by McFate 7 · 4 0

um ...
int whereYouWantToStop = 10; //whatever you want
int startingPoint = startingPoint; //whatever you declare
int nextNumber = 0;
int firstPreviousNumber = 1;
int secondPreviousNumber = startingPoint ;

out.write(String.valueOf(startingPoint ));
for(int x = 1; x <= whereYouWantToStop; x++){

nextNumber = firstPreviousNumber + secondPreviousNumber ;
firstPreviousNumber = nextNumber;
secondPreviousNumber = firstPreviousNumber;
out.write( String.valueOf( nextNumber));

}

//java

2007-08-07 10:07:49 · answer #5 · answered by Big D 4 · 0 0

Try this to start:

http://en.literateprograms.org/Fibonacci_numbers_(Java)

This should do 10, but should easily be upscaled for your needs.

2007-08-07 09:25:30 · answer #6 · answered by matthew_hetland797 3 · 0 1

fedest.com, questions and answers