# Need help with java question

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 05-17-2010, 05:23 PM
ccie007
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.
• 05-17-2010, 05:36 PM
curmudgeon
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.
• 05-17-2010, 07:41 PM
iluxa
that's easy enough:

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```
• 05-17-2010, 07:44 PM
Sno
Quote:

Originally Posted by iluxa
that's easy enough:

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
• 05-17-2010, 08:16 PM
iluxa
this is how you do it with loops:

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"...
• 05-17-2010, 08:19 PM
Sno
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??
• 05-17-2010, 09:12 PM
Norm
Whoops almost did it
• 05-18-2010, 04:22 PM
ccie007
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);
}

}

}
• 05-18-2010, 04:26 PM
m00nchile
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:
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.
• 05-18-2010, 04:27 PM
ccie007
I tried above code iluxa but it didn't work for me.
• 05-18-2010, 04:31 PM
m00nchile
Quote:

Originally Posted by ccie007
I tried above code iluxa but it didn't work for me.

You do realize that was a joke response?
• 05-18-2010, 04:33 PM
Norm
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?
• 05-18-2010, 04:56 PM
ccie007
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.
• 05-18-2010, 05:01 PM
m00nchile
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.
• 05-18-2010, 05:11 PM
ccie007
Hi Norm,
My program displays:
http://i.domaindlx.com/cobainia/fib.jpg
• 05-18-2010, 05:14 PM
ccie007
okay moonchile now i got it.lol
• 05-18-2010, 05:22 PM
ccie007
Hi Norm,
I am very weak in java.Could you write the code for me plz?
• 05-18-2010, 05:59 PM
curmudgeon
Quote:

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.
• 05-18-2010, 06:09 PM
ccie007
Thank you for the tuff love.I found a solution on the google.:)
• 05-18-2010, 06:13 PM
iluxa
Quote:

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!
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last