
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Common Divisors of Two Numbers in Java
In this article, we will learn to find the common divisors of two numbers using Java. The program will use a recursive method to calculate the greatest common divisor (GCD) of the two numbers, and then determine how many divisors are shared by both numbers. The output will display the total number of common divisors.
Problem Statement
Write a Java program to find and count the common divisors of two given numbers. Below is a demonstration of the same ?
Input
val_1= 68
val_2= 34
Output
The common divisors between the two numbers is
4
Steps to find the common divisors of two numbers
Following are the steps to find the common divisors of two numbers ?
- Define a class named Demo with static methods.
- Create a find_gcd method to calculate the GCD of two numbers using recursion.
- Create a common_divisors method to calculate common divisors using the GCD.
- Use a for loop to iterate through numbers from 1 to the square root of the GCD.
- Use an if-else statement to check if the GCD is divisible by the current number:
- If divisible and the divisor equals the quotient, increment the count by 1.
- Otherwise, increment the count by 2.
- In the main method, initialize two numbers and call the common_divisors method.
- Print the total number of common divisors.
Java program for common divisors of two numbers
Below is an example of common divisors of two numbers ?
public class Demo{ static int find_gcd(int val_1, int val_2){ if (val_1 == 0) return val_2; return find_gcd(val_2%val_1,val_1); } static int common_divisors(int val_1,int val_2){ int no = find_gcd(val_1, val_2); int result = 0; for (int i=1; i<=Math.sqrt(no); i++){ if (no%i==0){ if (no/i == i) result += 1; else result += 2; } } return result; } public static void main(String args[]){ int val_1 = 68, val_2 = 34; System.out.println("The common divisors between the two numbers is "); System.out.println(common_divisors(val_1, val_2)); } }
Output
The common divisors between the two numbers is 4
Code explanation
A class named Demo contains a static function that takes two values and returns the greatest common divisor using recursion. Another function calls this greatest common divisor function and iterates through numbers between 1 and square root of the greatest common divisor.
Next, the condition is that the number modulus of the values iterated should be equal to zero and next, the number divided by the values iterated should be equal to the value iterated, then the result (which was initialized to 0 initially) is incremented by 1. If the condition is not satisfied, then the result is incremented by 2. In the main function, two values are initialized, and the above function is called on it. Relevant result is displayed on the screen.