Monday, January 18, 2021
Home C / C++ Finding Greatest Common Divisor of 2 numbers in C++

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.

- Advertisment -

Most Popular

Android Webview Tutorial With Example | How to Use Webview in Android

Android Webview Tutorial With Example Have you seen an Android app displaying Web content using Webview. This Android Webview Tutorial is about integrating Webview using Android Studio....

Student Registration Project in PHP

In this Student Registration Project we are providing a form in simple html to the student and that form sends the data to "student.php"...

Java GUI Calculator Source Code

Here we have a Java GUI Calculator Source Code for you in which a Java GUI calculator has been created using "swing" and "awt". The calculator has...

Numeric Array in PHP

Numeric arrays can store numbers, strings and any object but their index will be represented by numbers. By default array index starts from zero....