Wednesday, October 12, 2011

Program code for calculate the LCM and HCF of any set of positive integers.(Using CPP Languge)

#include<iostream.h>
#include<conio.h>

void main()
{
    clrscr();
    int a,b,i,lcm=1,hcf=1,c,d;

    cout<<"Enter First numbers:>";
    cin>>c;
    cout<<"Enter Secound numbers:>";
    cin>>d;
    a=c;
    b=d;

    for(i=2;i>0;++i)
    {
        A: if(a%i==0)
        lcm*=i;
          if(b%i==0)
          {
            b=b/i;
            if(a%i!=0)
            lcm*=i;
          }

            if(a%i==0)
        a=a/i;
          if(a==1&&b==1)
            break;
          if(a%i==0||b%i==0)
            goto A;
    }

    for(i=2;i>0;++i)
    {
            B: if(c%i==0&&d%i==0)
        hcf*=i;
        if(c%i==0)
            c=c/i;
        if(d%i==0)
            d=d/i;
        if(c%i==1&&d%i==1)
        break;
        if(c%i==0||d%i==0)
        goto B;
    }
    cout<<"\nThe LCM is =:>"<<lcm;
    cout<<"\nThe HCF is =:>"<<hcf;

    getch();
}
         OUTPUT
Enter First numbers:>2
Enter Secound numbers:>8

The LCM is =:>8
The HCF is =:>2