Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default 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. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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. #4
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    Quote Originally Posted by iluxa View Post
    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
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  5. #5
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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. #6
    Sno's Avatar
    Sno
    Sno is offline Senior Member
    Join Date
    Apr 2010
    Posts
    196
    Rep Power
    5

    Default

    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??
    :rolleyes: ~ Sno ~ :rolleyes:
    '-~ B.S. Computer Science ~-'

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default

    Whoops almost did it

  8. #8
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    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. #9
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  10. #10
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    I tried above code iluxa but it didn't work for me.

  11. #11
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    I tried above code iluxa but it didn't work for me.
    You do realize that was a joke response?
    Ever seen a dog chase its tail? Now that's an infinite loop.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,448
    Rep Power
    25

    Default

    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. #13
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    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. #14
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  15. #15
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Hi Norm,
    My program displays:

  16. #16
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    okay moonchile now i got it.lol

  17. #17
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Hi Norm,
    I am very weak in java.Could you write the code for me plz?

  18. #18
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by ccie007 View Post
    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. #19
    ccie007 is offline Senior Member
    Join Date
    May 2010
    Posts
    112
    Rep Power
    0

    Default

    Thank you for the tuff love.I found a solution on the google.:)

  20. #20
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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 LastLast

Similar Threads

  1. Java Question :D
    By thisisIT in forum New To Java
    Replies: 6
    Last Post: 03-12-2010, 04:04 PM
  2. Java question
    By TGH in forum New To Java
    Replies: 12
    Last Post: 11-27-2009, 02:05 PM
  3. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM
  4. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 04:04 PM
  5. Java Question, i need to be answered
    By Sunshine in forum New To Java
    Replies: 7
    Last Post: 04-28-2008, 01:00 PM

Posting Permissions

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