
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find HCF (Highest Common Factor) of 2 Numbers in C++
In this tutorial, we will be discussing a program to find HCF (highest common factor) of two numbers.
For this we will be provided with two numbers. Our task is to find the highest common factor (HCF) of those numbers and return it.
Example
#include <stdio.h> //recursive call to find HCF int gcd(int a, int b){ if (a == 0 || b == 0) return 0; if (a == b) return a; if (a > b) return gcd(a-b, b); return gcd(a, b-a); } int main(){ int a = 98, b = 56; printf("GCD of %d and %d is %d ", a, b, gcd(a, b)); return 0; }
Output
GCD of 98 and 56 is 14
- Related Articles
- C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
- Program to find highest common factor of a list of elements in Python
- Haskell Program to calculate the Highest Common Factor
- Find the greatest common factor (GCF/HCF) of the polynomial $7x, 21x^2$ and $14xy^2$.
- Find the greatest common factor (GCF/HCF) of the polynomials $x^3$ and $-yx^2$.
- Finding two numbers given their sum and Highest Common Factor using JavaScript
- Find the greatest common factor (GCF/HCF) of the polynomials $15a^3, -45a^2$ and $-150a$.
- Find the greatest common factor (GCF/HCF) of the polynomial $6x^2y^2, 9xy^3$ and $3x^3y^2$.
- Find the highest common factor of;$7x^2yz^4, 21x^2y^5z^3$
- Find the greatest common factor (GCF/HCF) of the polynomials $a^2b^3$ and $a^3b^2$.
- Find the greatest common factor (GCF/HCF) of the polynomial $9x^2, 15x^2y^3, 6xy^2$ and $21x^2y^2$.
- Find the greatest common factor (GCF/HCF) of the polynomials $2x^3y^2, 10x^2y^3$ and $14xy$.
- Find the greatest common factor (GCF/HCF) of the polynomial $12ax^2, 6a^2x^3$ and $2a^3x^5$.
- Find the greatest common factor (GCF/HCF) of the polynomials $14x^3y^5, 10x^5y^3$ and $2x^2y^2$.
- Program to find GCD or HCF of two numbers in C++

Advertisements