Program to find GCD or HCF of two numbers in C++
https://www.tutorialspoint.com/program-to-find-gcd-or-hcf-of-two-numbers-in-cplusplus
-
10-09-2020 - |
문제
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
제휴하지 않습니다 Tutorialspoint