- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to calculate the GCD of a given number using recursion
You can calculate the GCD of given two numbers, using recursion as shown in the following program.
Example
import java.util.Scanner; public class GCDUsingRecursion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter first number :: "); int firstNum = sc.nextInt(); System.out.println("Enter second number :: "); int secondNum = sc.nextInt(); System.out.println("GCD of given two numbers is ::"+gcd(firstNum, secondNum)); } public static int gcd(int num1, int num2) { if (num2 != 0){ return gcd(num2, num1 % num2); } else{ return num1; } } }
Output
Enter first number :: 625 Enter second number :: 125 GCD of given two numbers is ::125
- Related Articles
- Java program to calculate the power of a Given number using recursion
- Swift program to find the GCD of two given numbers using recursion
- Haskell Program to find the GCD of two given numbers using recursion
- Java program to find the factorial of a given number using recursion
- C++ program to Calculate Factorial of a Number Using Recursion
- Java Program to calculate the power using recursion
- Java program to calculate the factorial of a given number using while loop
- Swift program to find the reverse of a given number using recursion
- Haskell Program to find the reverse of a given number using recursion
- Java Program to Find Factorial of a Number Using Recursion
- How to find the GCD of Two given numbers using Recursion in Golang?
- Write a Golang program to find the factorial of a given number (Using Recursion)
- Golang Program to Calculate The Power using Recursion
- Haskell Program to calculate the power using Recursion
- Java Program to Find Sum of Digits of a Number using Recursion

Advertisements