Calculate the GCD and LCM of a Number



Source code :-

#include<stdio.h>
int gcd(int x,int y)
{
    if(x%y==0)
        return y;
    else
        gcd(y,x%y);
}
int main()
{
    int x,y,g,l;
    printf("Enter the first number: ");
    scanf("%d",&x);
    printf("\nEnter the second number: ");
    scanf("%d",&y);
    if(x>y)
        g=gcd(x,y);
    else
        g=gcd(y,x);
    l=(x*y)/g;
    printf("\nGcd of two numbers %d and %d is :%d\n",x,y,g);   
    printf("\nLcm of two numbers %d and %d is :%d\n",x,y,l);
    return 1;
}

Input-Output :-

abhi@hp-15q-laptop:~$ gcc gcdlcm.c
abhi@hp-15q-laptop:~$ ./a.out
Enter the first number: 30
Enter the second number: 50
Gcd of two numbers 30 and 50 is :10
Lcm of two numbers 30 and 50 is :150

Post a Comment

Previous Post Next Post