Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to find common divisors of two numbers in cpp

// C++ implementation of program
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate gcd of two numbers
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
  
// Function to calculate all common divisors
// of two given numbers
// a, b --> input integer numbers
int commDiv(int a, int b)
{
    // find gcd of a, b
    int n = gcd(a, b);
  
    // Count divisors of n.
    int result = 0;
    for (int i = 1; i <= sqrt(n); i++) {
        // if 'i' is factor of n
        if (n % i == 0) {
            // check if divisors are equal
            if (n / i == i)
                result += 1;
            else
                result += 2;
        }
    }
    return result;
}
  
// Driver program to run the case
int main()
{
    int a = 12, b = 24;
    cout << commDiv(a, b);
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: string in int in cpp 
Cpp :: c++ Testing implementation details for automated assessment of sorting algorithms 
Cpp :: move semantics in c++ 
Cpp :: 10^18 data type in c++ 
Cpp :: https://www.cplusplus.com/doc/tutorial/pointers/ 
Cpp :: overloading templates in cpp 
Cpp :: second smallest element in array using one loop 
Cpp :: private access specifier in c++ program 
Cpp :: array di struct 
Cpp :: c++ iterator shorthand 
Cpp :: how to point to next array using pointer c++ 
Cpp :: cpp pointer to two dimensional array 
Cpp :: sento freddo a un dente 
Cpp :: how initilaize deffult value to c++ class 
Cpp :: rgb(100,100,100,0.5) validation c++ 
Cpp :: solve diamond inheritance c++ 
Cpp :: c++ define function in header 
Cpp :: cudaMalloc 
Cpp :: c++ comments 
Cpp :: c++ first index 0 or 1 
Cpp :: Arduino C++ servomotor random moving 
Cpp :: C++ Function-style Casting 
Cpp :: c+ - Dormir en millisecondes 
Cpp :: variabili in c++ 
Cpp :: how the theam are store in database 
Cpp :: C++ if...else Statement 
Cpp :: sprintf add two xeroes for a float number 
Cpp :: Magical Doors codechef solution in c++ 
Cpp :: rotateArray 
Cpp :: c++ else 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =