C Program to Find GCD and LCM of Number using Function

C Program to Find GCD and LCM of Number using Function:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().



#include<stdio.h>
main()
{
    int a,b;
    int lcm(int,int);
    int gcd(int,int);
    printf("Enter two numbers to find LCM and GCD\n");
    scanf("%d%d",&a,&b);

    printf("GCD of %d and %d is %d\n",a,b,gcd(a,b));
    printf("LCM of %d and %d is %d\n",a,b,lcm(a,b));
}

int gcd(int a,int b)
{
    int temp=0,gcd;
    while((b%a)!=0)
    {
       temp=b%a;
       b=a;
       a=temp;
    }
    return(a);
}
int lcm(int a,int b)
{
    int temp=0;
    temp=a*b/gcd(a,b);
    return(temp);
}

OUTPUT:

Enter two numbers to find LCM and GCD
10 45
GCD of 10 and 45 is 5
LCM of 10 and 45 is 90

Leave a Reply

Your email address will not be published. Required fields are marked *

6 + sixteen =