- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find GCD or HCF of two numbers in C++

In this tutorial, we will be discussing a program to find GCD and HCF of two numbers.

For this we will be provided with two numbers. Our task is to find the GCD or HCF (highest common factor) for those given two numbers.

## Example

#include <iostream> using namespace std; int gcd(int a, int b){ if (a == 0) return b; if (b == 0) return a; 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; cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b); return 0; }

## Output

GCD of 98 and 56 is 14

- Related Questions & Answers
- Java program to find the GCD or HCF of two numbers
- Program to find GCD or HCF of two numbers using Middle School Procedure in C++
- How to Find HCF or GCD using Python?
- Java Program to Find GCD of two Numbers
- Find GCD of two numbers
- Find HCF of two numbers without using recursion or Euclidean algorithm in C++
- C++ Program for GCD of more than two (or array) numbers?
- C++ Program to Find GCD of Two Numbers Using Recursive Euclid Algorithm
- C++ Program for GCD 0.of more than two (or array) numbers?
- GCD of more than two (or array) numbers in Python Program
- Python Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
- Program to find GCD of floating point numbers in C++
- Program to find HCF (Highest Common Factor) of 2 Numbers in C++
- C program to find GCD of numbers using recursive function

Advertisements