5 Replies Latest reply: Mar 25, 2008 1:37 PM by 807591 RSS

    Use a void method as int?

    807591
      See i have a program to compute hcf and lcm.
      Im using a void method to calculate hcf, so it prints it to the user.Now in the LCM, I use this method to get the hcf of the numbers, and then divide their product by it to get lcm.
      I declared the hcf an instance variable , but i need to make sure the hcf is computed before lcm is asked, so i call hcf from lcm
      The problem is the user will see both hcf and lcm when asking for lcm. How do i fix this?
      Code:
      public class HCFNLCM
      {
          int max;
          public void hcf(int a, int b)
          {
              max=1;
              for (int i=2;i<=Math.min(a,b);i++)
              {
                  if (a%i==0 && b%i==0)
                  {
                      if (i>max)
                      max=i;
                  }
              }
              System.out.println("HCF: "+max);
          }    
            
          public void lcm(int a, int b)
          {
              hcf(a,b);
              System.out.println("LCM: "+((a*b)/max));
          }
      }