Java Program to find Common Divisors

You are Here:

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, 61, 2
12, 241, 2, 3, 4, 6, 12
15, 25, 501, 5

Common Divisors of Two Numbers

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

Example

Java Compiler
public class myClass { public static void main(String[] args) { int num1 = 12; int num2 = 24; int i, min; min = (num1 < num2) ? num1 : num2; System.out.format("Common Divisors of %d and %d:\n", num1 , num2); for(i=1; i<=min; i++) { if((num1 % i == 0) && (num2 % i == 0)) System.out.print(i + " "); } } }

Output

Common Divisors of 12 and 24: 1 2 3 4 6 12

Common Divisors of any Two Given Numbers

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

Example

Java Compiler
import java.util.Scanner; public class myClass { public static void main(String[] args) { int num1, num2, i, min; Scanner reader = new Scanner(System.in); System.out.print("Enter (int) num1 = "); num1 = reader.nextInt(); System.out.print("Enter (int) num2 = "); num2 = reader.nextInt(); min = (num1 < num2) ? num1 : num2; System.out.format("\nCommon Divisors of %d and %d:\n", num1 , num2); for(i=1; i<=min; i++) { if((num1 % i == 0) && (num2 % i == 0)) System.out.print(i + " "); } } }

Output

Enter (int) num1 = 7 Enter (int) num2 = 14 Common Divisors of 7 and 14: 1 7

Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.

We are working to cover every Single Concept in Java.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author