NEW 4″ Multi-Touch Android 4.0 Smart Phone Dual SIM WIFI Unlocked AT&T T-Mobile
Current Bid: $63.96
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 != 0
Below 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
Java Video Tutorials
Java Programming Video Training tutorials CBT 30+ Hrs
Current Bid: $4.99
Advanced Java Programming Video Training tutorials CBT – 30+ Hrs – Master level
Current Bid: $4.99
Java Books
Core Java 2 Advanced Features Vol. II by Gary Cornell and Cay Horstmann…
Current Bid: $3.99
No Photo
(2 December, 2004) Core Java(TM) 2, Volume II–Advanced Features (7th Edition)
Current Bid: $10.00
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