- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Java Source code on Printing the Greatest Common Divisor (GCD) using Recursion
Get A Website Plus a Free Domain Name in Just 1 Hour!
Bring the new technology in your hands! Share your skills, improve and impress. Get Your Own Website and a Free Domain Name Here!
Below is the Java sample code on printing the GCD of the 2 numbers in recursion. The GCD recursive methods is quite an easy problem to program if you know its recursive definition. Since recursion is basically has a base case and general case, the program will stop on looping if it meets the condition of the base case and return a value of the general case if not. To have a glimpse of the recursive formula, here it is for you to evaluate and see how I transform it into java language.
GCD Recursive Definition:
Given two integers x and y
{ x if y==0
GCD(x, y) =
{ y if y != 0Below is the following code for the recursive definition above.
Java Source Code on Printing the Greatest Common Divisor or GCD in Recursion
//java class
public class GCD
{
public static int gcd(int num1, int num2)
{
if(num2 == 0)
{
return num1;
}
else
{
return gcd(num2, num1%num2);
}
}
}
//main class
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the base number: ");
int num1 = input.nextInt();
System.out.print("Enter the power: ");
int num2 = input.nextInt();
GCD access = new GCD();
System.out.print("The GCD of 2 numbers is: " + access.gcd(num1, num2));
}
}
Java Tutorials and Tips
- Complete List of Java Tutorial and Source Codes for Absolute Beginners
- 5 Important Tips to Learn Java Programming and Other Programming Languages
- Basic Knowledge Required in Programming
- How to Program in Java: Complete Simple Easy Steps
- Java Simple Codes for Beginners
- Java Tutorial for Beginners: A Beginners Guide on Learning Java Programming
- Java Class: Learn More About Classes in Java
Here is the sample output of the program
Sample Output:
Enter the base number: 100
Enter the power: 45
The GCD of 2 numbers is: 5
Sample Output 2:
Enter the base number: 6
Enter the power: 8
The GCD of 2 numbers is: 2
Other Java Source Code Examples
- Java Simple Codes for Beginners
- What is a Java Scanner Class and How to Use it?
- Java Program: How to Use Switch Statement in Java
- Java source codes on outputting Asterisk in Different Forms Using Recursion
- Java Source Code: A Recursive Asterisk Diamond Shape
- Java Source code on Adding Numbers inside the Array using For Loop
- Java Source codes on Adding numbers inside the Array Using Recursion
- Java Source Code: How to Sort Numbers in Bubble Sort Using Recursion
- Java Source Code: Sort Numbers using Selection Sort
- Java Source Code on Linear Search Using Recursion
- Java Source Code: Binary Search in Recursion
- Java Source code: How to Print a String Backward using Recursion
- Java source code: How to Output the Answer of the Integer X to the Power Y
- Java Source Code: How to make a Program that will determine a Person's Salutation and Current Age
- Java Source Code: Recursive Snow Flakes
