HCF and LCM

View previous topic View next topic Go down

HCF and LCM

Post by Sujith on Tue Jul 06, 2010 9:45 pm

Code:

/* Program to print HCF and LCM of 2 numbers*/
class HCFandLCM
{
    public static void lcm(int a, int b)
    {
    int big=(a>b)?a:b;    //Bigger number
    int small=(a>b)?b:a; // smaller number
        for(int i=1;i<=big;i++)
        {
            if(((big*i)%small)==0)
            {
                int lcm=big*i;
                System.out.println("The least common multiple is "+(lcm));
                break;
            }
        }
    hcf(big, small);  // Arguments sent to hcf() function
    }
    public static void hcf(int bigC, int smallC)
    {
        int temp=1;
        while(temp!=0)
        {
            temp=bigC%smallC;
            if(temp==0)
            {
                System.out.println("Highest Common Factor is "+smallC);
            }
            else
            {
                bigC=smallC;
                smallC=temp;
            }
        }
    }
    public static void main(String[] args)
    {
    int x=Integer.parseInt(args[0]);
    int y=Integer.parseInt(args[1]);
    lcm(x, y);
    }
}

Variable Table


Variable NameData TypeDescription
a,b,bigC,smallC
int
Formal Parameters
big, small
int
Used to calculate the hcf and lcm
x,y
int
Inputs, Actual Parameters
temp
int
Temporary variable

______________________________________________________
It is the simplest things that are most Complicated!
Been on this forum since 2639 days!

avatar
Sujith
LSF Expert
LSF Expert

Posts : 126

http://osalfps.org

Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum