Thursday, February 16, 2017

Greatest Common Divisor(GCD) of given 2 numbers

Write a C program to find Greatest Common Divisor (GCD) of given 2 numbers.
GCD is also known as Greatest Common Factor (GCF)
GCD is also known as Highest Common Factor (HCF)

#include <stdio.h>
int main()
{
  int num1, num2, i, j, temp, gcd;

  printf("Enter number 1:");
  scanf("%d", &i);

  printf("Enter number 2:");
  scanf("%d", &j);

  num1 = i;
  num2 = j;

  while (num2 != 0)
  {
    temp = num2;
    num2 = num1 % num2;
    num1 = temp;
  }

  gcd = num1;
  printf("Greatest Common Divisor(GCD) of %d and %d is %d.\n", i,j,gcd);
  return 0;
}

Output of program

Enter number 1:20
Enter number 2:30
Greatest Common Divisor(GCD) of 20 and 30 is 10.


No comments:

Post a Comment