ADVERTISEMENT
ADVERTISEMENT

C program to find the GCD (Greatest Common Divisor) of two integers

Here, we are going to learn how to find the GCD (Greatest Common Divisor) of two integers using C program?
Submitted by Nidhi, on August 03, 2021

Problem Solution:

Read two integer numbers, and find the Greatest Common Divisor of given numbers.

Program:

The source code to find the GCD (Greatest Common Divisor) of two integers is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

// C program to find the GCD 
// (Greatest Common Divisor) of two integers

#include <stdio.h>

int main()
{
    int num1 = 0;
    int num2 = 0;
    int rem = 0;
    int X = 0;
    int Y = 0;

    printf("Enter Number1: ");
    scanf("%d", &num1);

    printf("Enter Number2: ");
    scanf("%d", &num2);

    if (num1 > num2) {
        X = num1;
        Y = num2;
    }
    else {
        X = num2;
        Y = num1;
    }

    rem = X % Y;

    while (rem != 0) {
        X = Y;
        Y = rem;
        rem = X % Y;
    }

    printf("Greatest Common Divisor is: %d\n", Y);

    return 0;
}

Output:

RUN 1:
Enter Number1: 10
Enter Number2: 20
Greatest Common Divisor is: 10

RUN 2:
Enter Number1: 10
Enter Number2: 225
Greatest Common Divisor is: 5

RUN 3:
Enter Number1: 110
Enter Number2: 17
Greatest Common Divisor is: 1

Explanation:

Here, we read two integer numbers num1 and num2 from the user and found the Greatest Common Divisor of both numbers, and printed the result on the console screen.

C Basic Programs »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.