Ask Question - Get Answer

3 Ans Three ways to find Greatest Common Divisor (GCD) and Least Common Multiple (LCM) in a C program

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

Please log in to answer, like and save
0
Save 0

<<< Previous
Log in to Answer Next >>>

Answer(s):

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, 04:30 PM

Please log in to Upvote, Downvote and Report

#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, 04:30 PM

Please log in to Upvote, Downvote and Report

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, 04:30 PM

Please log in to Upvote, Downvote and Report
           

Related Q/A:

2 Ans BPSC Programming Aptitude Lab Test Important programs

2 Ans Write a C program to implement Selection sort data structure and algorithm

1 Ans Write a C program to implement x power n using recursion

1 Ans How to Learn HTML - HyperText Markup Language easily? 

1 Ans A C Program to split a number into digits

1 Ans Write a C program to check a Prime number

1 Ans 41st BCS Technical Subjective Written Exam Question Download

1 Ans Best Web development site in the internet ?

1 Ans Write a C program to find the highest and lowest number in a series

1 Ans Write a C program to implement Insertion sort data structure and algorithm