Greatest Common Divisor: It is the highest number that completely divides two or more numbers. It is abbreviated for GCD. It is also known as the Greatest Common Factor (GCF) and the Highest Common Factor (HCF).
Example: Find the GCF of 12 and
Solution:
Factors of 12: 1, 2, 3, 4, 6, 12
Factors of 8: 1, 2, 4, 8
Common Factors: 1, 2, 4
Greatest Common Factor: 4
Hence, the GCF of 12 and 8 is 4.
//JAVA Program to ind GCD of two numbers
package Diploma;
import java.util.Scanner;
public class GCD
{
public static void main(String args[])
{
int x, y, i, gcd = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter first number :: ");
x = sc.nextInt();
System.out.println("Enter second number :: ");
y = sc.nextInt();
for(i = 1; i <= x && i <= y; i++)
{
if(x%i==0 && y%i==0)
gcd = i;
}
System.out.println("GCD = "+gcd);
}
}
No comments:
Post a Comment