CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    1

    Begginer with Methods

    Hey, i have a question involving methods. This program is supposed to take the largest common multiple from 2 numbers. I have no clue if i am on the right track but the main thing i need to know is how to get it to output the number that I get from the "public static int lcm".





    public class TestLCM
    {

    public static void main(String[] args)
    {
    lcm(6, 8);
    }

    public static int lcm (int number1, int number2)
    {
    int iAdd1 = number1;
    int iAdd2 = number2;

    for (int iLoop = 1; iLoop <= 100; iLoop++)
    {
    number1= number1 + iAdd1;
    number2= number2 + iAdd2;
    if (number1 == number2) break;

    }
    return number1;
    }


    }

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Begginer with Methods

    Assign the returned value to a variables. Your method returns whatever the value for number1 is within the method but the main method does not know what to do with it because you are not storing it.

    Code:
    int answer = lcm(6,8)
    Within your main you can now use variable answer and it will be the value returned by the lcm method. By the way you may want to check your LCM method since it will never return the lcm.

    Code:
    //Loop iteration 1
    6 = 6 + 6;
    8 = 8 + 8;
    //Loop iteration 2
    12 = 12 + 6;
    16 = 16 + 8;
    //Loop iteration 3
    18 = 18 + 6;
    24 = 24 + 8;
    //Loop iteration 4
    24 = 24 + 6;
    32 = 32 + 8;
    As you can see the LCM should be 24 but since they are at 24 at different loop iterations it will never break out of the loop until it reaches 100 iterations.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured