C++ Program to find HCF and LCM of two numbers by calculating factors [DEVCPP/GCC]

PROGRAM

//Program to find HCF and LCM of two numbers by calculating factors

#include<iostream>

using namespace std;

int main()
{
       int a,b,arr1[100],arr2[100],i,j,hcf,lcm,count1=0,count2=0;

       cout<<"*********HCF & LCM OF TWO NUMBERS**********\n\n";

       cout<<"ENTER FIRST NUMBER: ";
       cin>>a;
       cout<<"ENTER SECOND NUMBER: ";
       cin>>b;

       // Finding factors and storing them in array

       cout<<"\n\nFACTORS OF "<<a<<" ARE: ";
       for(i=1 ; i<=a ; i++)
       {
             if(a%i==0)
             {
                 arr1[count1]=i;
                 cout<<arr1[count1]<<" ";
                 count1++;
             }
       }

       cout<<"\nFACTORS OF "<<b<<" ARE: ";
       for(i=1 ; i<=b ; i++)
       {
              if(b%i==0)
              {
                  arr2[count2]=i;
                  cout<<arr2[count2]<<" ";
                  count2++;
              }
       }

       //Checking highest common factor
     
       for(i=0;i<=count1;i++)
       {
  for(j=0;j<=count2;j++)
  {
      if(arr1[j]==arr2[i])
      {
hcf=arr1[j];
                     }
  }
       }

       cout<<"\n\nHCF of "<<a<<" & "<<b<<" : "<<hcf;
       lcm=(a*b)/hcf;
       cout<<"\nLCM of "<<a<<" & "<<b<<" : "<<lcm;
       return 0;
}

OUTPUT









FIND US ON FACEBOOK!