Skip to content
This repository has been archived by the owner on Jun 14, 2024. It is now read-only.

HCF & LCM #156

Closed
devmalik7 opened this issue Dec 9, 2020 · 8 comments
Closed

HCF & LCM #156

devmalik7 opened this issue Dec 9, 2020 · 8 comments

Comments

@devmalik7
Copy link

Write a program to find the HCF and LCM of the given number without using the multiplication and division.

@ghost
Copy link

ghost commented Dec 9, 2020 via email

@Sooraj2002
Copy link

I want to work on this issue.

@devmalik7
Copy link
Author

@Sooraj2002 sure

@Sooraj2002
Copy link

//PROGRAM TO FIND HCF OF TWO NO'S
#include<stdio.h> #include<conio.h> int main() { int num1, num2, mp; printf("Enter two Numbers: "); scanf("%d %d", &num1, &num2); if(num1>num2) mp=num1; else mp=num2; while(1) { if((num1%mp==0) && (num2%mp==0)) break; else mp--; } printf("\nHCF(%d,%d) = %d", num1, num2, mp); getch(); return 0; }

@heyyayesh
Copy link

heyyayesh commented Jan 14, 2021

//Program to find the LCM of two numbers

#include <stdio.h>

int main(){
int n1, n2, x;
printf("Enter two numbers: \n");
scanf("%d %d", &n1, &n2);

if(n1==n2){
    printf("LCM is %d.\n", n1);
    return 0;
}

x = (n1>n2) ? n1 : n2;

for(int i = 1; i <= n1*n2; i++, x++){
    if(x % n1 == 0 && x % n2 == 0){
        printf("LCM is %d.\n", x);
        break;
    }
}

return 0;

}

@B2B-Rohit
Copy link

//Program to find the LCM of two numbers

#include <stdio.h>

int main(){
int n1, n2, x;
printf("Enter two numbers: \n");
scanf("%d %d", &n1, &n2);

if(n1==n2){
    printf("LCM is %d.\n", n1);
    return 0;
}

else if x = (n1>n2) ? n1 : n2;/should be i think/

for(int i = 1; i <= n1*n2; i++, x++){
    if(x % n1 == 0 && x % n2 == 0){
        printf("LCM is %d.\n", x);
        break;
    }
}

return 0;

}

@shreegilliorkar
Copy link

/Write a program to find the HCF and LCM of the given number without using the multiplication and division./

#include<bits/stdc++.h>
using namespace std;
int multiply(int firstnum, int secondnum){

// If second number is zero then the product is zero.

if(secondnum == 0){
    return 0;
} else {

   // Add first num, until second num is equal to zero.
   return (firstnum + multiply(firstnum, secondnum-1));
}

}

int divide(int x, int y){
// handle divisibility by 0
if (y == 0)
{
printf("Error!! Divisible by 0");
exit(1);
}

// store sign of the result
int sign = 1;
if (x * y < 0) {
    sign = -1;
}

// convert both dividend and divisor to positive
x = abs(x), y = abs(y);

// initialize quotient by 0
int quotient = 0;

// loop till dividend `x` becomes less than divisor `y`
while (x >= y)
{
    x = x - y;      // perform a reduction on the dividend
    quotient++;     // increase quotient by 1
}

return sign * quotient;

}

// Driver code
#include <stdio.h>
int main() {
int a, b, x, y, t, hcf, lcm;

printf("Enter two integers\n");
scanf("%d%d", &x, &y);

a = x;
b = y;

while (b != 0) {
t = b;
b = a % b;
a = t;
}

hcf = a;
lcm = divide(multiply(x,y),hcf);

printf("Highest Common Factor of %d and %d = %d\n", x, y, hcf);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

return 0;
}

@shreegilliorkar
Copy link

You can try this out.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants