Program to find GCD of two numbers in C language

The following program is to find the GCD or HCF of a number using C Language. GCD is the Greatest Common Divisor that divides a particular number.

#include<stdio.h>
void main(){
int a,b,i,j;
printf("\n****************************\nprogram to find GCD of 2 Numbers\n*****************************\n\n");
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);

printf("Common Factors for %d and %d are: ",a,b);

j=a>b?b:a;
for(i=1;i<=j;i++){
if(a%i==0 && b%i==0){
printf("%d ", i);
}
}
printf("\n\n");
for(i=j;i>=1;i--){
if(a%i==0 && b%i==0){

break;}

}
printf("GCD for %d and %d is %d\n\n",a,b,i);

}

OUTPUT:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.