Write a C program to find the GCD of two integer values.

Greatest common divisor (gcd), also known as the greatest common factor (gcf), or highest common factor (hcf), of two or more integers (at least one of which is not zero), is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4. Procedure 1:
int main(){

    int x,y,m,i;

    printf("Insert any two number: ");

    scanf("%d%d",&x,&y);
    if(x>y)
         m=y;
    else
         m=x;

    for(i=m;i>=1;i--){
         if(x%i==0&&y%i==0){
             printf("\nHCF of two number is : %d",i) ;
             break;
         }
    }
    return 0;
}
Procedure 2:
int main()
{
     int n1,n2;
     printf("\nEnter two numbers:");
     scanf("%d %d",&n1,&n2);
     while(n1!=n2)
     {
         if(n1>=n2-1)
              n1=n1-n2;
         else
              n2=n2-n1;
     }
     printf("\nGCD=%d",n1);
   return 0;
}

No comments:

Post a Comment