gcd and lcd in c 1

gcd and lcd in c

#include 
//here is a c programme which can find GCD and LCD

int main(){

    int n1,n2,num1,num2,rem,gcd,lcm;

    printf("Enter first and second number number (3,4) : \n");
    scanf("%d %d",&num1,&num2);
    n1=num1;
    n2=num2;

    while(n2!=0){
        rem=n1%n2;
        n1=n2;
        n2=rem;

    }

    gcd=n1;
    lcm=(num1*num2)/gcd;

    printf("GCD of %d and %d is %d\n",num1,num2,gcd);
    printf("LCM of %d and %d is %d\n",num1,num2,lcm);


    return 0;
}

Here is what the above code is Doing:
1. First, we take two numbers from the user.
2. Then, we store the value of the first number in n1 and the second number in n2.
3. Now, we use a while loop to find the GCD of the two numbers.
4. Inside the while loop, we find the remainder of n1 divided by n2.
5. Then, we store the value of n2 in n1 and the remainder in n2.
6. We continue this process until n2 becomes 0.
7. After the while loop, the value of n1 is the GCD of the two numbers.
8. Finally, we find the LCM of the two numbers using the formula:

LCM = (num1 * num2) / GCD

Similar Posts