C Program to find the GCD of two numbers.
GCD of two numbers :
GCD.c
#include<stdio.h>
int gcdCalc(int a,int b);
int main(void)
{
int x, y;
printf("Enter x and y values to calculate GCD : \n");
scanf("%d%d",&x, &y);
printf("%d\n",gcdCalc(x,y));
return 0;
}
int gcdCalc(int a,int b)
{
int rem;
while(b!=0)
{
rem=a%b;
a=b;
b=rem;
}
return a;
}
Output:
Terminal
Enter x and y values to calculate GCD :
25
60
5
Enter x and y values to calculate GCD :
800
100
100
GCD of two numbers Recursive :
GCDRecursive.c
#include<stdio.h>
int GCD_Recursive(int a,int b);
int main(void)
{
int x, y;
printf("Enter x and y values to calculate GCD : \n");
scanf("%d%d",&x, &y);
printf("%d\n",GCD_Recursive(x,y));
return 0;
}
int GCD_Recursive(int a,int b)
{
if(b==0)
return a;
return GCD_Recursive(b, a%b);
}
Output:
Terminal
Enter x and y values to calculate GCD :
800
100
100
Enter x and y values to calculate GCD :
350
50
50
Happy Learning 🙂