Find HCF of Two Numbers in C

Google Advertisements

Prev Tutorial Next Tutorial

Find HCF of Two Numbers Program in C

To find the HCF (Highest Common Factor) of two or more numbers, make prime factors of the numbers and choose the common prime factors. Then the take the highest common factor this highest common factor is hcf of number. For example;

Find HCF of Two Numbers Program in C

Suppose find HCF of 4 and 8
4=4*1=2*2
8=8*1=2*4=2*2*2
Factor of 4 is 1, 2, 4
Factor of 8 is 1, 2, 4, 8
So, common factor of 4 and 8 is 1, 2, 4
and highest common factor is 4
hcf of 4 and 8 is: 4

program to find hcf of two numbers

#include<stdio.h>
#include<conio.h>

void gcd(int,int);

void main()
{
    int a,b;
    clrscr();
    printf("Enter two numbers: ");
    scanf("%d %d",&a,&b);
    gcd(a,b);
    getch();
  //  return 0;
}

//function to calculate g.c.d
void gcd(int a,int b)
{
    int m,n;

    m=a;
    n=b;

    while(m!=n)
    {
        if(m>n)
            m=m-n;
        else
            n=n-m;
    }

    printf("\nH.C.F of %d and %d is: %d",a,b,m);
}

Output

Enter any two number: 
3
15
H.C.F of 3 and 15 is: 3

Prev Tutorial Next Tutorial

Google Advertisements

We have more than 5 Groups which have 500000+ Members and some Pages have more than 100000+ members. For Advertisement contact on [email protected] or 8076671483

Buy This Ad Space @$50 per Month, Ad Size 600X200 Contact on: [email protected] or 8076671483

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Classpath variable is set for providing a path for predefined Java classes which is used in our application. All classes are available in lib/rt.jar so we set classpath upto lib/rt.jar. Difference between path and classpath in Java

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: [email protected] or 8076671483 Try this Keyword C++ Programs