AMCAT Coding Questions asked in Programming Section Prime Numbers

Program to find GCD (HCF)


/**
 * C program to find HCF(Highest Common Factor) of two numbers
 */
 
#include <stdio.h>
 
int main()
{
    int i, num1, num2, min, hcf=1;
 
    /*
     * Reads two numbers from user
     */
    printf("Enter any two numbers to find HCF: ");
    scanf("%d %d", &num1, &num2);
 
    min = (num1<num2) ? num1 : num2;
 
    for(i=1; i<=min; i++)
    {
        /*
         * If i is factor of both number
         */
        if(num1%i==0 && num2%i==0)
        {
            hcf = i;
        }
    }
 
    printf("HCF of %d and %d = %d\n", num1, num2, hcf);
    return 0;
}


Output
Enter any two numbers to find HCF: 12
30
HCF of 12 and 30 = 6

No comments:

Post a Comment