"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Wednesday, February 24, 2010

WAP to calculate the H.C.F and L.C.M of three numbers.

eg.  H.C.F of 11,22,33 is 11
      L.C.M of 11,22,33 is 66

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

     H.C.F of 2,3,4 is 1
     L.C.M of 2,3,4 is 24

#include<iostream.h>
#include<conio.h>
void main()
{
int a, b, c; // three numbers
int hcf,lcm;

//calculate the minimum of three numbers
int min = (a < b ? a : b);
min = (min < c ? min : c);

for( int i = min; i>0; i--)
{
if(a%i == 0 && b%i == 0 && c%i == 0)
{
  hcf = i;
  break;
  }
}

lcm = (a*b*c)/hcf;

cout<<" \n The H.C.F of " << a <<" , " << b << " , " << c << "is " << hcf;

cout<<" \n The L.C.M of " << a <<" , " << b << " , " << c << "is " << lcm;

getch();

}  

/*  Similarly you can calculate HCF of 2 numbers or 4 numbers and so on. */

2 comments:

  1. u shud hav to input 3 nos, otherwise the programming runs wrong.....

    ReplyDelete
  2. C++ Program to Find HCF of two numbers

    To find the HCF or GCD of two or more numbers, make prime factors of the numbers and choose the common prime factors. Then the take the highest common factor this highest common factor is HCF of number.

    ReplyDelete

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.