# Thread: Need help with java question

1. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0

## Need help with java question

Hi all,
Can anybody resolve this question.Thank you in advance.I need help plz.

In mathematics, there is a famous sequence of numbers called the Fibonacci
sequence after the thirteenth-century Italian mathematician Leonardo Fibonacci. The
first two terms in this sequence are 0 and 1, and every subsequent term is the sum of
the preceding two. Thus the first several numbers in the Fibonacci sequence are as
follows:
F0 = 0
F1 = 1
F2 = 1 (0 + 1)
F3 = 2 (1 + 1)
F4 = 3 (1 + 2)
F5 = 5 (2 + 3)
F6 = 8 (3 + 5)
Write a program to display the values in this sequence from F0 through F15.

2. Senior Member
Join Date
May 2010
Posts
436
Rep Power
7
You'll get better help if you do more than simply post your homework assignment without asking a question. To help folks know where you're stuck, please post what you've tried so far and any specific questions. The more specific the question, the more helpful the answer.

3. Senior Member
Join Date
Mar 2010
Posts
266
Rep Power
7
that's easy enough:

Java Code:
```System.out.println("0");
System.out.println("1");
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("5");
System.out.println("8");
// keep going until you have 15 printouts```

4. Originally Posted by iluxa
that's easy enough:

Java Code:
```System.out.println("0");
System.out.println("1");
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("5");
System.out.println("8");
// keep going until you have 15 printouts```
haha, that works too!

also, try using for loops and incrementing

5. Senior Member
Join Date
Mar 2010
Posts
266
Rep Power
7
this is how you do it with loops:

Java Code:
```String [] fibonacci = {"0", "1", "1", "2", "3", "5", "8", }; //continue until at least 100 strings

for (int i = 0; i < requestedNumber; i ++) {
System.out.println (fibonacci [i]);
}```
i'm on the roll today. next question please.

PS. what particularly cracks me up is the shear wealth of information you get by googling "java fibonacci"...

6. I believe there is one more way you can do this with a for loop and two incrementing integers. I'm at work, Who can do it??

7. Whoops almost did it

8. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
Mine doesn't work but this what I have done so far.

/*File:chapt4ques9.java
* This program displays sequence of numbers called the Fibonacci
sequence
* */

import acm.program.*;

public class chapt4ques9 extends ConsoleProgram{

public void run(){

int sum=0;

for (int x= 0; x <=1; x++){

for (int i =0; i <15; i++)

println("F"+x+"="+i);
}

}

}

9. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
7
I'll give you a big hint. Using recursion is a pretty logical way of calculating the fibonacci sequence. I'll even give a "fill in the blanks" type help:
Java Code:
```int fib(int n) {
if(n //what goes here) return n;
return fib(//hmm)+fib(//hmm indeed);
}```
Then it's just a matter of looping to get the output required.

10. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
I tried above code iluxa but it didn't work for me.

11. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
7
Originally Posted by ccie007
I tried above code iluxa but it didn't work for me.
You do realize that was a joke response?

12. Now you need to design the logic to compute the series. This is done with paper and pencil.
Looking at what you want for output,i t appears that F0 and F1 are the initial input
then F2 = F0 + F1
and F3 = F2 + F1
Then replace the fixed subscripst with new names:
Fnew = FOld + FOld-1
then move the values thru the variables. Ie Fold = Fnew and FOld-1 = FOld

F0 = 0
F1 = 1
F2 = 1 (0 + 1)
F3 = 2 (1 + 1)

Why do you have nested if statements in your program segment?
What does your program print out?

13. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
Sorry Moonchile I meant to say I tried iluxa's code but didn't work form.We haven't studied recursion on java yet.Hence It sounds advance for me.Is there other way using if,while,etc loops we can resolve it.

14. Senior Member
Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
7
What I meant was that the code Iluxa posted was "joke code". It's not a solution, if you look at it, it doesn't do any calculations, just prints out preset strings. If recursion is too advanced for you, there were other responses in this thread that give you hints at solving this problem.

15. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
Hi Norm,
My program displays:

16. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
okay moonchile now i got it.lol

17. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
Hi Norm,
I am very weak in java.Could you write the code for me plz?

18. Senior Member
Join Date
May 2010
Posts
436
Rep Power
7
Originally Posted by ccie007
Hi Norm,
I am very weak in java.Could you write the code for me plz?
No, no one is going to write the code for you, nor should they. This site is for teaching folks to learn Java, not a "do my homework for me" site. Please do your own work.

19. Senior Member
Join Date
May 2010
Posts
112
Rep Power
0
Thank you for the tuff love.I found a solution on the google.:)

20. Senior Member
Join Date
Mar 2010
Posts
266
Rep Power
7
Thank you for the tuff love.I found a solution on the google.
That's fantastic news! As long as you promise to never attempt to get a job in computers, that's definitely the way to go!

Page 1 of 2 12 Last

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•