- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 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
Advertisements