Write a program that will find the GCD of two numbers.

What is GCD?

GCD is the acronym for Greatest Common Divisor. It is also called as HCF which is the acronym for Highest Common Factor. GCD of any two numbers is the largest number that divides both of the numbers
To find GCD we find any largest number which can divide both the given number.

Examples:

For example: To find GCD/ HCF of 20 and 30.
Both number 20 and 30 are divisible by 1, 2,5,10.
The GCD is 10.

Solution: C

//A program to find the GCD of two numbers
//Important question for Vyom Labs Interview Preparation

#include <stdio.h>
 
long findGcd(long, long);
 
int main() {
  long x, y, gcd;
 
  printf("Enter two integers\n");
  scanf("%ld%ld", &x, &y);
 
  gcd = findGcd(x, y);
 
  printf("Greatest common divisor of %ld and %ld = %ld\n", x, y, gcd);
  
  return 0;
}
 
long findGcd(long a1, long a2) {
  if (a2 == 0) {
    return a;
  }
  else {
    return findGcd(a2, a1 % a2);
  } 
} 

HR Interview

Technical Interview

Important Tips