Program to find LCM and HCF of two numbers using loops in C | Wave the world

Program to find LCM and HCF of two numbers using loops in C

Hi Friends,
     Before wirting the program, I will tell you what is LCM (Lowest Common Multiple) and HCF (Highest Common Factor). If you already know what is LCM and HCF then you can skip the theory and redirect to program.

HCF is the highest positive number that divides all given number for which you are calculating HCF. It is multiplication of common factor of given numbers. i.e.

90 = 1 * 2 * 3 * 3 * 5
70 = 1 * 2 * 5 * 7

Hence the HCF of 90 and 70 will be 1 * 2 * 5 = 10

LCM is the smallest positive number that can be divided by all the given numbers. It is the least common multiple of given numbers. i.e.

90 = 90 , 180 , 270 , 360 , 450 , 540, 630 ,720 , 810 , 900 , 990 ..... ..... .... ....
70 = 70 , 140 , 210 , 280 , 350 , 420, 490, 560 , 630 , 700 , 770 .... .... .... .....

Hence the LCM of 90 and 70 will be 63.

/*
     Program to implement LCM , HCF using loops
     Created By : Pirate
*/

#include<stdio.h>
#include<conio.h>
int hcf(int a,int b);
int lcm(int a,int b);
int main(){
    int number_a,number_b;
    printf("Enter the two no\n");
    scanf("%d%d",&number_a,&number_b);
    printf("\nHighest Common Factor HCF is %d\n", hcf(number_a,number_b));
    printf("\nLowest Common Multiple LCM is %d", lcm(number_a,number_b));
    return 0;
}
int hcf(int a,int b){
    int temp;
    while(b != 0){
        temp = b;
        b = a % b;
        a = temp;
    }
    return temp;
}
int lcm(int a, int b){
    int temp;
    temp = a > b ? a : b;
    while(1){
        if(temp % a == 0 && temp % b == 0){
            return temp;
        }
        temp++;
    }
}


Output :


Enjoy :)



1 comments:

  1. <Investing online has been a main source of income, that's why knowledge plays a very important role in humanity, you don't need to over work yourself for money.All you need is the right information, and you could build your own wealth from the comfort of your home!Binary trading is dependent on timely signals, assets or controlled strategies which when mastered increases chance of winning up to 90%-100% with trading. It’s possible to earn $10,000 to $20,000 trading weekly-monthly in cryptocurrency(bitcoin) investment,just get in contact with Mr Bernie Doran my broker. I had almost given up on everything and even getting my lost funds back, till i met with him, with his help and guidance now i have my lost funds back to my bank account, gained more profit and I can now trade successfully with his profitable strategies and software!! 
Reach out to him through Gmail : Bernie.doranfx01@gmail.com ,Telegram: bernie_fx or WhatsApp +1(424)285-0682 for inquires

    ReplyDelete

 

Pro

About