Lucas/Fibonacci sequence HELP please!!
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Lucas/Fibonacci sequence HELP please!!

  1. #1
    Join Date
    Jun 2011
    Posts
    5

    Lucas/Fibonacci sequence HELP please!!

    I am having trouble with my following code in which I have to write a program that allows the user to request a Lucas number (similar to Fibonacci sequence) and then my program is supposed to output that requested Lucas number & the calculated Lucas number.
    **However, my problem is that my program is only outputting the requested # and instead of calculating the Lucas number, the Lucas number is always 0!! So, I'm figuring that I have an error in my Lucas code. If someone has any suggestions......I've been working on this code for days & I'm sure it's a simple mistake?!

    *I have to write one static method in addition to main
    *Use loops to solve the Lucas number series
    *Use the double data type to process large Lucas numbers.

    ----I think I have most of that, however it will not calculate the Lucas number correctly for some reason?? PPPppplease help!!
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2011
    Posts
    5

    Re: Lucas/Fibonacci sequence HELP please!!

    Import java.util.Scanner;

    Public class Lucas

    {

    public static void main ( String args [] )

    {

    System.out.println ("Which Lucas number would you like? ");

    Scanner input = new Scanner ( System.in );

    double num = input.nextDouble();

    while ( num < 0 || num > 76 )



    {

    System.out.print ("Which Lucas number would you like? ");

    num = input.nextInt();

    }

    System.out.printf ("Lucas #%d is %s\n", num, getLucas);

    }

    public static int getLucas(double result)

    {

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Lucas/Fibonacci sequence HELP please!!

    Quote Originally Posted by ckinser View Post
    System.out.printf ("Lucas #&#37;d is %s\n", num, getLucas);
    The program doesn't look complete. Does it compile at all?

    The above print statement isn't consistent with how getLucas is defined. GetLucas is expected to return a String (%s) but it's defined to return an int. And it isn't called with any parameters at all although it's defined to be passed a double.
    Last edited by nuzzle; June 21st, 2011 at 03:08 AM.

  4. #4
    Join Date
    Jun 2011
    Posts
    5

    Re: Lucas/Fibonacci sequence HELP please!!

    public class Lucas

    {

    public static void main ( String args [] )

    {

    System.out.println ("Which Lucas number would you like? ");

    Scanner input = new Scanner ( System.in );

    double num = input.nextDouble();

    while ( num < 0 || num > 76 )



    {

    System.out.print ("Which Lucas number would you like? ");

    num = input.nextInt();

    }

    System.out.printf ("Lucas #%d is %s\n", num, getLucas);

    }

    public static int getLucas(double result)

    {

    double pre = 2;

    result = 1;

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

    {

    int sum = result + pre;

    pre = result;

    result = sum;

    }

    return result;

    }

    }

  5. #5
    Join Date
    Jun 2011
    Posts
    5

    Re: Lucas/Fibonacci sequence HELP please!!

    The above code is my code thus far. When I run it it does compile, however it outputs as "Lucas #7 is 0" or any # that the user inputs it will output back but calculate the Lucas number as 0.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Lucas/Fibonacci sequence HELP please!!

    Well, as I said the printf statement is fishy. It should look something like this to fit with the getLucas definition,

    System.out.printf ("Lucas #%d is %d\n", num, getLucas(num));

    I changed %s to %d because getLucas returns an int.

    and I changed getLucas to getLucas(num) because it takes a double parameter.

  7. #7
    Join Date
    Jun 2011
    Posts
    5

    Re: Lucas/Fibonacci sequence HELP please!!

    i'm receiving the error message "identifier expected"
    on this line
    public static int getLucas(num)

  8. #8
    Join Date
    Jun 1999
    Location
    SW Missouri
    Posts
    3,422

    Re: Lucas/Fibonacci sequence HELP please!!

    When you post errors: Please post the FULL text of the error message.

    public static int getLucas(num)
    You need to tell the compiler the type of the variable: num

    For example:
    public static int getLucas(int num)
    Norm

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    Re: Lucas/Fibonacci sequence HELP please!!

    The most immediate problem is that the num variable isn't defined in getLucas.

    public static int getLucas(double result)

    to

    public static int getLucas(double num)

    and

    result = 1;

    to

    int result = 1;

    It may still not compile but then the issue is the way you mix doubles and ints.

  10. #10
    Join Date
    May 2009
    Posts
    2,413

    Re: Lucas/Fibonacci sequence HELP please!!

    Quote Originally Posted by ckinser View Post
    public static int getLucas(num)
    I didn't notice but why the f... did you change the getLucas definition?

    If you want guidance you need to follow the instructions and I told you to change the getLucas call in the printf statement.

  11. #11
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: Lucas/Fibonacci sequence HELP please!!

    Quote Originally Posted by ckinser View Post
    When I run it it does compile, however...
    Compiling & running are two separate things. You compile the .java source code, which creates a .class file. This .class file is what you run. If compilation failed (which it does when you have syntax errors), then no .class file would have been created so you wouldn't have been able to run anything. (EDIT: be careful though, a previous .class file may still exist).

    Even if you are able to successfully compile, it doesn't guarantee that the program is correct. It just means that there were no syntax errors. But there could still be logic errors that cause the program to run, but not behave the way you want it to,.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center