C program to find HCF (Highest Common Factor) of two numbers

Learn, how to find HCF of two numbers using C program?

Problem statement

Given two numbers, write a C program to find their HCF (Highest Common Factor).

C program to find HCF (Highest Common Factor) of two numbers

/*C program to find HCF of two numbers.*/
#include <stdio.h>
 
//function to find HCF of two numbers
int findHcf(int a,int b)
{
    int temp;
     
    if(a==0 || b==0)
    return 0;
    while(b!=0)
    {
        temp = a%b;
        a    = b;
        b    = temp;
    }
    return a;
}
int main()
{
    int a,b;
    int hcf;
     
    printf("Enter first number: ");
    scanf("%d",&a);
    printf("Enter second number: ");
    scanf("%d",&b);
     
    hcf=findHcf(a,b);
    printf("HCF (Highest Common Factor) of %d,%d is: %d\n",a,b,hcf);
     
    return 0;
}

Output

Enter first number: 100
Enter second number: 40
HCF (Highest Common Factor) of 100,40 is: 20

C Basic Programs »


Related Programs

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.