### 3 AnsThree ways to find Greatest Common Divisor (GCD) and Least Common Multiple (LCM) in a C program Write Answer Report

Asked by AL MaMun (4 Golds) Friday, 31 Jul 2020, 06:29 AM at (Education Tutorials)

 Like 0 Save 0

 <<< Previous Write a C program to print Floyd Triangle Log in to Answer Next >>> Write a C program to find the highest and lowest number in a series

### A C program to find GCD#include <stdio.h>int main(){    int a, b, t, x, gcd;    scanf("%d %d", &a, &b);    if (a == 0) gcd = a;    else if (b == 0) gcd = b;    else    {        while (b != 0)        {            t = b;            b = a % b;            a = t;        }        gcd = a;    }    printf("GCD is %d\n", gcd);    return 0;}

Answered by AL MaMun (4 Golds) Friday, 31 Jul 2020, 06:30 AM

 Upvote 0 Downvote 0 Report 0

### #include <stdio.h>int main(){    int a, b, x, gcd;    scanf("%d %d", &a, &b);    if (a < b)    {        x = a;    }    else    {        x = b;    }    for(; x >= 1; x--)    {        if (a % x == 0 && b % x == 0)        {            gcd = x;            break;        }    }    printf("GCD is %d\n", gcd);    return 0;}

Answered by AL MaMun (4 Golds) Friday, 31 Jul 2020, 06:30 AM

 Upvote 0 Downvote 0 Report 0

### GCD and LCM using recursion#include<stdio.h>int gcd(int a, int b){    if(a%b==0) return b;    return gcd(b, a%b);}int main(){    int a, b, result;    printf("Enter a and b\n");    scanf("%d%d", &a, &b);    result=gcd(a,b);    printf("GCD = %d\n", result);    printf("LCM = %d", (a*b)/result);    return 0;}

Answered by AL MaMun (4 Golds) Friday, 31 Jul 2020, 06:30 AM

 Upvote 0 Downvote 0 Report 0