A positive integer d is a common divisor of a and b when it divides both numbers with remainder 0. The largest common divisor is the GCD (greatest common divisor).
Remember
d is a common divisor of a and b
⇔ a % d == 0 and b % d == 0
Key fact:
common divisors of (a, b) = all divisors of gcd(a, b)
Example: a = 12, b = 18
gcd = 6
common divisors = 1, 2, 3, 6
a
b
Common divisors
GCD
12
18
1, 2, 3, 6
6
7
13
1
1
24
36
1, 2, 3, 4, 6, 12
12
Approach
How to Find Common Divisors in C#
Four clear steps — then pick a basic loop or a GCD-based list.
Take absolute values of a and b (divisors are about magnitude).
Reject the case where both values are 0 (no finite list).
Either scan 1 … min(a, b), or compute gcd and list its divisors.
Print the common divisors (and optionally the GCD).
Method
Idea
Time
Best for
Naive loop
Test every i from 1 to min(a, b)
O(min)
Interviews, small inputs
GCD + divisors
Find g = gcd(a, b), then list divisors of g
O(log min + √g)
Larger inputs, optimizations
Pseudocode
Pseudocode
function CommonDivisors(a, b):
a = abs(a)
b = abs(b)
if a == 0 and b == 0:
reject input
limit = min(a, b) // if one is 0, use the other
if limit == 0:
limit = max(a, b)
result = []
for i from 1 to limit:
if a mod i == 0 and b mod i == 0:
result.append(i)
return result
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Test
a % d == 0 && b % d == 0
Upper bound
Scan only to min(|a|, |b|)
GCD shortcut
Common divisors = divisors of gcd(a, b)
Always included
1 (unless both are 0)
Largest
gcd(a, b) itself
Classic check
12, 18 → 1, 2, 3, 6
Try it
Live Preview
Enter two integers to see their GCD and every common divisor.
Whole numbers (absolute values used). Tap a chip or type a pair.
Result
a = 12, b = 18
gcd(12, 18) = 6
Common divisors: 1, 2, 3, 6
Count = 4
Trace
Worked Walkthrough — a = 12, b = 18
Check every candidate from 1 through min(12, 18) = 12. Keep only values that divide both.
Calculation
i=1 → 12%1==0 and 18%1==0 → keep
i=2 → 12%2==0 and 18%2==0 → keep
i=3 → 12%3==0 and 18%3==0 → keep
i=4 → 12%4==0 but 18%4!=0 → skip
i=5 → skip
i=6 → 12%6==0 and 18%6==0 → keep
i=7..12 → skip
Common divisors: 1, 2, 3, 6
gcd = 6
Code
C# Programs
Start with the readable loop to min(a, b). Then use GCD plus a divisor scan for a faster interview answer.
Example 1 — Simple Loop
Test every integer from 1 to min(a, b) and print those that divide both.
C#
using System;
class Program
{
static void PrintCommonDivisors(int a, int b)
{
a = Math.Abs(a);
b = Math.Abs(b);
if (a == 0 && b == 0)
{
Console.WriteLine("No finite common divisors when both are 0.");
return;
}
int limit = Math.Min(a, b);
if (limit == 0)
{
limit = Math.Max(a, b);
}
Console.Write("Common divisors: ");
for (int i = 1; i <= limit; i++)
{
if (a % i == 0 && b % i == 0)
{
Console.Write(i + " ");
}
}
Console.WriteLine();
}
static void Main()
{
PrintCommonDivisors(12, 18);
}
}
Output
Common divisors: 1 2 3 6
How It Works
Take absolute values so negative inputs behave like positives.
If both are 0, stop — there is no finite list of common divisors.
Set limit to the smaller magnitude (or the nonzero value if one input is 0).
For each i from 1 to limit, print i only when a % i == 0 and b % i == 0.
For 12 and 18, the kept values are 1, 2, 3, 6.
Example 2 — GCD Method
Compute gcd(a, b), then list every divisor of that GCD using pairs up to √g.
C#
using System;
using System.Collections.Generic;
class Program
{
static int Gcd(int x, int y)
{
x = Math.Abs(x);
y = Math.Abs(y);
while (y != 0)
{
int temp = y;
y = x % y;
x = temp;
}
return x;
}
static List<int> CommonDivisors(int a, int b)
{
int g = Gcd(a, b);
List<int> result = new List<int>();
if (g == 0)
{
return result; // both zero — empty list
}
for (int i = 1; i <= g / i; i++)
{
if (g % i == 0)
{
result.Add(i);
int partner = g / i;
if (partner != i)
{
result.Add(partner);
}
}
}
result.Sort();
return result;
}
static void Main()
{
List<int> divisors = CommonDivisors(12, 18);
Console.WriteLine("gcd = " + Gcd(12, 18));
Console.WriteLine(string.Join(", ", divisors));
}
}
Output
gcd = 6
1, 2, 3, 6
How It Works
Gcd uses the Euclidean algorithm: replace (x, y) with (y, x % y) until y is 0.
For 12 and 18, that yields g = 6.
Every common divisor must divide 6, so it is enough to list divisors of 6.
The loop runs to √g. When i divides g, add both i and g / i (once if they are equal).
Sort the list for a clean interview-style answer: 1, 2, 3, 6.
Edge Cases & Pitfalls
Check these before calling the solution done.
Both 0
No finite list
Reject or document the case. Do not loop forever.
One 0
Use the nonzero value
Common divisors are exactly the divisors of the nonzero number.
Negatives
Take Math.Abs
Positive divisors depend on magnitude, not sign.
Coprime
Only 1
Pairs like 7 and 13 share only 1.
Analysis
Time and Space Complexity
Program
Time
Extra space
Naive loop to min(a, b)
O(min(a, b))
O(1) (printing only)
GCD + divisor pairs
O(log min + √g)
O(d) for the list (d = divisor count)
g is gcd(|a|, |b|).
Remember
Key Takeaways
Definition:d divides both a and b.
GCD shortcut: common divisors = all divisors of gcd(a, b).
Two loops: naive to min, or Euclidean GCD then √g pairs.
Test set:12, 18 → 1 2 3 6; 7, 13 → 1.
One line: values that divide both numbers — or simply every divisor of their GCD.
Frequently Asked Questions
A positive integer d is a common divisor of a and b when a % d == 0 and b % d == 0. For 12 and 18, the common divisors are 1, 2, 3, and 6.
Loop from 1 to min(a, b) and collect values that divide both, or compute gcd(a, b) and list the divisors of that gcd.
Every common divisor of a and b divides gcd(a, b), and every divisor of gcd(a, b) is a common divisor. So listing divisors of the gcd is enough.
Yes for any pair that is not both zero. The number 1 divides every integer.
The naive loop is O(min(a, b)). Computing gcd is O(log min), then listing its divisors is O(sqrt(g)) with the pair method.