Q:

C++ Program to Calculate HCF of Two Numbers using Functions

belongs to collection: C++ Number Solved Programs

Write a C++ Program to Calculate HCF of Two Numbers using Functions . Here’s simple C++ Program to Calculate HCF of Two Numbers using Functions in C++ Programming Language.

HIGHEST COMMON FACTOR (H.C.F)

• The HCF of two (or more) numbers is the largest number that divides evenly into both numbers.
• In other words the H.C.F is the largest of all the common factors.
• The common factors or of 12 and 18 are 1, 2, 3 and 6.
• The largest common factor is 6, so this is the H.C.F. of 12 and 18.
• It is very easy to find a H.C.F. of small numbers, like 6 and 9 (it is 3) or 8 and 4 (it is 4).
• The best way is to keep finding the factors of the smaller number, starting from the largest factor. The first factor of the smaller number that is also a factor of the larger number is a H.C.F.

Here is source code of the C++ Program to Calculate HCF of Two Numbers using Functions. The C++ program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.

SOURCE CODE : :

``````/*  C++ Program to Calculate HCF of Two Numbers using Functions  */

#include<iostream>
using namespace std;

void gcd(int,int);

int main()
{
int a,b;

cout<<"Enter 1st number :: ";
cin>>a;
cout<<"\nEnter 2nd number :: ";
cin>>b;

gcd(a,b);

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<<" ] and [ "<<b<<" ] is :: "<<m<<"\n";
}``````

Output : :

```/*  C++ Program to Calculate HCF of Two Numbers using Functions  */

Enter 1st number :: 12

Enter 2nd number :: 30

H.C.F of [ 12 ] and [ 30 ] is :: 6

Process returned 0```

Above is the source code for C++ Program to Calculate HCF of Two Numbers using Functions which is successfully compiled and run on Windows System.The Output of the program is shown above .