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, 06:29 AM 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, 06:30 AM

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, 06:30 AM

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, 06:30 AM

Please log in to Upvote, Downvote and Report
           

Related Q/A:

1 Ans Write a C program to check a Palindrome Number

1 Ans Write a C program to print Pascal Triangle

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

1 Ans Write a C program for Binary Search algorithm

1 Ans Write a C program to check a perfect number

1 Ans Write a C program to check a Prime number

1 Ans Write a C program to find nth Fibonacci number

1 Ans Write a C program to check Bubble sort data structure and algorithm

1 Ans Write a C++ program for BigMod math

1 Ans Write a C program to print Fibonacci number series


AL MaMun active 2 days ago (4 Golds)
Birds of the sky (active on Sunday, 04 Apr 2021, 03:09 PM) (1 Gold)
Tajrimun Elisha (active on Friday, 22 Nov 2019, 08:56 AM) (2 Golds)
Hena Begum (active on Friday, 15 Feb 2019, 11:04 PM) (8 Bronzes)
Ghost (active on Saturday, 05 Jan 2019, 10:37 AM) (1 Silver)
APU KUMAR ROY (active on Sunday, 22 Sep 2019, 02:42 AM) (1 Bronze)
Md Hasan (active on Saturday, 02 Nov 2019, 10:02 AM) (4 Bronzes)
BrandonDug (active on Thursday, 17 Dec 2020, 05:25 AM) ( New Contributor )
Shahansha Habib (active on Monday, 21 Oct 2019, 04:09 PM) (2 Bronzes)
Hazrat (active on Sunday, 26 May 2019, 05:55 AM) (2 Bronzes)
عبد الحنان سلفي (active on Friday, 28 Aug 2020, 05:04 PM) (2 Bronzes)
Md Mondol (active on Tuesday, 23 Jul 2019, 10:28 PM) (2 Bronzes)
student (active on Monday, 27 Jul 2020, 07:28 PM) (2 Bronzes)
Sowrov (active on Thursday, 14 Feb 2019, 10:48 AM) (7 Bronzes)
Daily Science (active on Friday, 03 Jan 2020, 07:52 PM) ( New Contributor )
GoMedii (active on Monday, 21 Dec 2020, 02:28 PM) ( New Contributor )
Arif Hossain (active on Tuesday, 08 Jan 2019, 11:59 PM) (3 Silvers)
Apu Kumar Roy (active on Thursday, 28 Mar 2019, 07:05 PM) ( New Contributor )
Hr Hannan (active on Tuesday, 11 Dec 2018, 12:28 PM) (2 Bronzes)
Jackson99 (active on Monday, 27 Apr 2020, 02:44 PM) ( New Contributor )
Show All Members

Online Now 1
Today's Unique PageViews 244
Today's Total PageViews 742
Site's Total PageViews 2313362