C program to find the GCD and LCM using pointers

Here is a C program to find the Greatest common devisor and the Least common multiple of a number.

#include<stdio.h>
void gcdlcm(int num1,int num2,int*gcd,int*lcm)
{
    int i,max;
    max=(num1>num2)?num1:num2;//Simple way of saying if num1 is greater 
    //than num2 max=num1 else max=num2
    for(i=1;i<=max;i++)
    {
        if(num1%i==0&&num2%i==0)
            *gcd=i;
    }
    *lcm=(num1*num2);
}
int main(){
    int num1,num2,gcd,lcm;
    printf("Enter the first number:");
    scanf("%d",&num1);
    printf("Enter 2nd number\n");
    scanf("%d",&num2);
    gcdlcm(num1,num2,&gcd,&lcm);
    printf("Gcd of %d and %d is:%d",num1,num2,gcd);
    printf("lcm of %d and %d is:%d",num1,num2,lcm);
    return 0;
}

The main part of the code is.

 max=(num1>num2)?num1:num2;//Simple way of saying if num1 is greater 
    //than num2 max=num1 else max=num2
    for(i=1;i<=max;i++)
    {
        if(num1%i==0&&num2%i==0)
            *gcd=i;
    }
    *lcm=(num1*num2);
}

Hope this blog was helpfull. Dont forget to follow.

Thankyou...

0
Subscribe to my newsletter

Read articles from Gagan G Saralaya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gagan G Saralaya
Gagan G Saralaya