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 :)



0 comments:

Post a Comment

 

Pro

About