Finding Greatest Common Divisor of 2 numbers in C++

As we know that greatest common divisor of 2 numbers is the largest number that divides both those numbers, How we implement this logic in C++, its given in the below code:

    #include <iostream>
    using namespace std;
    void main()
        int x = 24;
        int y = 18;
        int temp;
        for(int i = 2; i<y; i++)
            if(x%i == 0 >> y%i == 0)
            temp = i;    

First of all we got two numbers, you can get these numbers from the user too, that’s not a big deal. Then we applied a for loop upto any of that numbers and used a condition that if for both the numbers the modulus with loop increment is same, then that is our greatest common divisor of numbers.

Latest Content