C++ Program to find Common Divisors

Last Updated:

What are Common Divisors?

A number that divides two or more numbers without remainder.

Tips: It is recommended to use our online common divisor calculator for better understanding.

Examples

The following table provides few examples of common divisors of the given numbers.

NumbersCommon Divisors
4, 6 1, 2
12, 24 1, 2, 3, 4, 6, 12
15, 25, 50 1, 5

Common Divisors of Two Numbers

In the following example, we will find the common divisors of numbers 12 and 24.

Example

C++ Compiler
#include <iostream> using namespace std; int main() { int num1 = 12; int num2 = 24; int i, min; min = (num1 < num2) ? num1 : num2; cout << "Common Divisors of " << num1 <<" and " << num2 <<":\n"; for(i=1; i<=min; i++) { if((num1 % i == 0) && (num2 % i == 0)) cout << i <<" "; } return 0; }

Common Divisors of any Two Given Numbers

In the following example, we will find the common divisors of any two given numbers.

Example

C++ Compiler
#include <iostream> using namespace std; int main() { int num1, num2, i, min; cout << "Enter (int) num1 = "; cin >> num1; cout << "Enter (int) num2 = "; cin >> num2; min = (num1 < num2) ? num1 : num2; cout << "\nCommon Divisors of " << num1 <<" and " << num2 <<":\n"; for(i=1; i<=min; i++) { if((num1 % i == 0) && (num2 % i == 0)) cout << i <<" "; } return 0; }

Share this Page

Meet the Author