Question 2.3: Create a function that takes two numbers as arguments and returns the GCD of the two numbers.
Examples
gcd(3, 5) ➞ 1
gcd(14, 28) ➞ 14
gcd(4, 18) ➞ 2
Solution:
package com.company;
import java.util.Scanner;
public class lab2problem03
{
public static void main(String[] args) {
Scanner Obj = new Scanner(System.in);
System.out.println("Input first integer:");
int n = Obj.nextInt();
System.out.println("Input Second integer: ");
int m = Obj.nextInt();
int gcd = 1;
for(int i = 1; i <= n && i <= m; ++i)
{
if(n % i==0 && m % i==0)
gcd = i;
}
System.out.println("G.C.D of and "+ gcd);
}
}