Wednesday, December 31, 2014

HCF of a Number in c program



Find HCF of a given Number without using Recursion

#include 
#include  
int hcf(int, int);
 
int main()
{
    int a, b, result;
    clrscr();
    printf("Enter the two numbers to find their HCF: ");
    scanf("%d%d", &a, &b);
    result = hcf(a, b);
    printf("The HCF of %d and %d is %d.\n", a, b, result);
 
    return 0;
}
 
int hcf(int a, int b)
{
    while (a != b)
    {
        if (a > b)
        {
            a = a - b;
        }
        else
        {
            b = b - a;
        }
    }
    return a;
}

Output
 
Enter the two numbers to find their HCF: 24 36
The HCF of 24 and 36 is 12.

 



No comments:

Post a Comment