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

By | 25.12.2016

HCF of Two Numbers using Functions 


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 .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

4.4 7 votes
Article Rating
Subscribe
Notify of
guest

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dicam

include<iostream>
using namespace std;

int hcf(int a, int b) 

if (b == 0) 
return a; 
return hcf(b, a % b);
}

int main()
{
int a,b;
cout<<“Enter two numbers:\n”;
cin>>a>>b;
cout<<“HCF of “<<a<<” and “<<b<<” is “<<hcf(a, b)<<endl; 
     
system(“pause”);
return 0;
}

shrived

plz make theme light

danial

#include <iostream>

using namespace std;
int HCF_Func(int, int);
int main()
{
  int a,b;
  int result;
  cout<<“give two number:”<<endl;
  cin>>a>>b;
  result= HCF_Func(a,b);
  cout<<“result is: “<<result;
  return 0;
}
int HCF_Func(int a, int b)
{
  int devide= a-1;
  int re1,re2;
  for(; devide>=1; devide–)
  {
    re1=a%devide;
    if( re1==0 )
    {
      re2=b%devide;
      if( re2==0 )
      {
        break;
      }
    }
  }
  return devide;
}

janah

the function should be before the main