-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCD.java
More file actions
42 lines (40 loc) · 1.19 KB
/
GCD.java
File metadata and controls
42 lines (40 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Program: Compute GCD Using Iteration and Recursion
// Topic: Recursion and Mathematical Algorithms
// Description: Reads two integers from user input and computes their Greatest Common Divisor (GCD) in two ways —
// using an iterative method `GCD()` and a recursive method `gcd()`. Demonstrates the Euclidean algorithm in both forms
// to highlight differences between iterative and recursive approaches for solving the same problem.
package recursion;
import java.util.*;
/**
*
* @author Samim
*/
public class GCD
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n1,n2;
System.out.println("Enter First Number :");
n1=sc.nextInt();
n2=sc.nextInt();
System.out.println(gcd(n1,n2));
System.out.println("Arithematic Logic :"+GCD(n1,n2));
}
public static int GCD(int a,int b){
while(b!=0){
int temp=b;
b=a%b;
a=temp;
}
return a;
}
public static long gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
}