C program to find the G.C.D. of two number.

Posted by Mangesh on March 16, 2018

/* Program to find Greatest Common Factor of two number. */

Description :

The greatest common divisor (GCD) of a set of integers is the largest integer that divides each integer in the set. It is also called Highest Common Factor.

Program :

#include<stdio.h>
#include<conio.h>
int gcd(int a,int b)
{
  if(a==0)
  return b;
  return gcd(b%a,a);
}
void swap(int a,int b)
{
  int temp;
  temp = a;
  a = b;
  b = temp;
}
void main()
{
  int x,y;
  printf("Enter first number : ");
  scanf("%d",&x);
  printf("Enter second number : ");
  scanf("%d",&y);
  if(y>x)
    swap(x,y);
  printf("Greatest Common Factor of %d and %d is %d.",x,y,gcd(x,y));
  getch();
}

Output :

C program for gcd` tested in Turbo C 3.2

Written with from Mangesh.

Related Post
1 C program to calculate factorial of given number.
2 C program to convert decimal number to binary number.
3 C program to find the G.C.D. of two number.
Latest Post
1 C program to implement Queue using linked list.
2 C program for binary search tree (BST).
3 C program to search an element in linked list.
4 C program for postorder traversal in binary tree.
5 C program for preorder traversal in binary tree.