C++ Program to Find GCD of Two Numbers

In this post, we will learn how to find the GCD of two numbers using C++ Programming language.

The greatest common divisor (GCD) of two nonzero integers ‘a‘ and ‘b‘ is the greatest positive integer ‘d‘ such that d is a divisor of both ‘a‘ and ‘b‘. The GCD of ‘a‘ and ‘b‘ is denoted by gcd(a, b). For example: The GCD of 6 and 9 is 3.

We will calculate the gcd of two numbers using the following approaches:

  1. Using For Loop 
  2. Using While Loop

So, without further ado, let’s begin this tutorial.

C++ Program to Find GCD of Two Numbers

// C++ Program to Find GCD of Two Numbers
#include <iostream>
using namespace std;

int main(){
    int a, b, gcd;
    
    // Asking for input
    cout << "Enter the first number: ";
    cin >> a;
    cout << "Enter the second number: ";
    cin >> b;
    
    // Calculating gcd of two numbers
    for (int i = 1; i <= a && i <= b; i++){
        if (a % i == 0 && b % i == 0){
            gcd = i;
        }
    }
    
    // Displaying output
    cout << "GCD of " << a << " and " << b << " is: " << gcd << endl;
    return 0;
}

Output

Enter the first number: 10
Enter the second number: 25
GCD of 10 and 25 is: 5

How Does This Program Work ?

    int a, b, gcd;

In this program, we have declared three integer data type variables named a, b and gcd.

    // Asking for input
    cout << "Enter the first number: ";
    cin >> a;
    cout << "Enter the second number: ";
    cin >> b;

The user is asked to enter two numbers.

    // Calculating gcd of two numbers
    for (int i = 1; i <= a && i <= b; i++){
        if (a % i == 0 && b % i == 0){
            gcd = i;
        }
    }

We used for loop to calculate the gcd of two numbers. If the iteration of i is divisible by both first number as well as second number, then it is a factor of both numbers.

The value of i keeps increasing until it is equal to either ‘a‘ or ‘b‘. The highest factor computed is the gcd of two numbers. The GCD of two numbers gets stored in the gcd named variable.

    // Displaying output
    cout << "GCD of " << a << " and " << b << " is: " << gcd << endl;

And then the greatest common divisor of ‘a‘ and ‘b‘ is displayed on the screen using the cout statement.

C++ Program to Find GCD of Two Numbers Using While Loop

// C++ Program to Find GCD of Two Numbers Using While Loop
#include <iostream>
using namespace std;

int main(){
    int a, b, temp, gcd;
    
    // Asking for input
    cout << "Enter the first number: ";
    cin >> a;
    cout << "Enter the second number: ";
    cin >> b;
    
    // Calculating gcd using while loop
    while (b != 0){
        temp = b;
        b = a % b;
        a = temp;
    }
    
    gcd = a;
    
    // Displaying output
    cout << "GCD of two numbers: " << gcd;
    return 0;
}

Output

Enter the first number: 21
Enter the second number: 28
GCD of two numbers: 7

Conclusion

I hope after going through this post, you understand how to find gcd of two numbers using C++ Programming language.

If you have any query regarding the post, then feel free to contact us in the comment section. We will be delighted to assist you.

Also Read:

Leave a Comment

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