C++ Program to Find GCD

Advertisements

String is Palindrome or Not in C++ LCM of Two Numner in C++

C++ Program to Find HCF of two numbers

To find the HCF (Highest Common Factor) or GCD (Greatest Common Divisor) 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 or GCD

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

C++ Program to Find GCD

#include<iostream.h>
#include<conio.h>

void gcd(int,int);

void main()
{
    int a,b;
    clrscr();
    cout<<"Enter two numbers: ";
    cin>>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;
    }

    cout<<"\nH.C.F of"<<a<<" & "<<b<<" is "<<m;
}

Output

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

String is Palindrome or Not in C++ LCM of Two Numner in C++
Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements