Monday, April 1, 2013

C++ program to find HCF and LCM of two numbers


Write a c++ program to find LCM of two numbers




# include <iostream>
# include <string >
using namespace std;
int main()
{
int a,b,c;
cout<< "Enter two nos :"<<endl;
cout<<endl;
cout<< "Enter first no. : ";
cin>>a;
cout<< "Enter sec. no. : ";
cin>>b;
c=a*b;
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
cout<< "HCF = " << a<<endl;
cout<< "LCM = " << c/a<<endl;
return 0;
}

---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
---------------------------------------------------
Write a c++ program to find LCM of two numbers 
I want a C++ program to find LCM and HCF of two 
Find hcf and lcm using recursive technique 
How To write a c program to calculate the lcm and hcf  
LCM And HCF  C++.Program to find HCF and LCM

7 comments:

  1. I don't quite understand why this program works. Please explain the logic behind this.

    ReplyDelete
    Replies
    1. This is the right program and work fine .find the LCM and HCF.Program written in Dev c++.


      #include
      #include
      using namespace std;
      main()
      {

      int a,b,hcf,lcm,max,min,r;
      cout<<"Enter two numbers:";
      cin>>a>>b;

      if(a>b)
      {
      max=a;
      min=b;
      }
      else
      if(b>a)
      {
      max=b;
      min=a;
      }

      if(a==b)
      hcf=a;
      else
      {
      do
      {
      r=max%min;
      max=min;
      min=r;
      cout<<"max"<<max<<"min"<<min<<endl;
      }while(r!=0);
      hcf=max;

      }

      lcm=(a*b)/hcf;
      cout<<"nLCM="<<lcm<<"nHCF="<<hcf;
      getch();
      }

      Delete
  2. This is the right program and work fine .find the LCM and HCF.Program written in Dev c++.


    #include
    #include
    using namespace std;
    main()
    {

    int a,b,hcf,lcm,max,min,r;
    cout<<"Enter two numbers:";
    cin>>a>>b;

    if(a>b)
    {
    max=a;
    min=b;
    }
    else
    if(b>a)
    {
    max=b;
    min=a;
    }

    if(a==b)
    hcf=a;
    else
    {
    do
    {
    r=max%min;
    max=min;
    min=r;
    cout<<"max"<<max<<"min"<<min<<endl;
    }while(r!=0);
    hcf=max;

    }

    lcm=(a*b)/hcf;
    cout<<"nLCM="<<lcm<<"nHCF="<<hcf;
    getch();
    }

    ReplyDelete
  3. This code works. And is very small, good thinking.

    ReplyDelete
  4. public class HCF {
    static int absFun(int num){
    return(num>0?num:(-num));
    }
    int calculateHCF(int x,int y){
    if((x!=0)&&(y!=0))
    return (x>y)?(calculateHCF(y,x%y)):(calculateHCF(x,y%x));
    return (x>y?x:y);
    }
    int getLCM(int x,int y,int i){
    return( ( ((x*i)%x)+((x*i)%y)==0 )?(x*i):getLCM(x, y, i+1));
    }
    int calculateLCM(int x,int y){
    int i=1;
    return(x>y?getLCM(x,y,i):getLCM(y,x,i));
    }
    public static void main(String[] args) {
    int x=15;
    int y=5;
    x=absFun(x);
    y=absFun(y);
    HCF obj=new HCF();
    int hcf=obj.calculateHCF(x, y);
    System.out.println("HCF:"+hcf);
    System.out.println("+++++++++++++++++");
    System.out.println("LCM="+obj.calculateLCM(x,y));
    }
    }

    visit more example

    ReplyDelete
  5. C++ Program to Find HCF of two numbers
    Thanks for sharing this code, It's very simple code. Every one can understand easily this program. Keep Sharing.

    ReplyDelete
  6. Its simple but it can't work in case of two prime numbers !

    ReplyDelete