Home » C Program to Find GCD or HCF of two Numbers using while loop

C Program to Find GCD or HCF of two Numbers using while loop

Learn about C Program to Find GCD or HCF of two Numbers using while loop in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Contents

C Program to Find GCD or HCF of two Numbers using while loop

Source code:

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

     printf("Enter two positive integers: ");
     scanf("%d %d", &num1, &num2);

     while(num1 != num2)
     {
         if( num1 > num2 )
             num1 -= num2; // num1= num1 - num2
         else
             num2 -= num1; //num2= num2 - num1
     }

     printf("GCD = %d\n",num1);

     return 0;
 }

Output:

Enter two positive integers: 20 25
GCD = 5

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
C Program to Find GCD or HCF of two Numbers
C Program to Find Power of a Number using pow() function