forked from philona/cppcodes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhcf.cpp
More file actions
41 lines (39 loc) · 1.06 KB
/
hcf.cpp
File metadata and controls
41 lines (39 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
using namespace std;
// Recursive function declaration
int findGCD(int, int);
// main program
int main()
{
int first, second;
cout<<“Enter First Number: “;
cin>>first;
cout<<“Enter second Number: “;
cin>>second;
cout<<“GCD of “<<first<<” and “<<second<<” is “<<findGCD(first,second);
return 0;
}
//body of the function
int findGCD(int first, int second)
{
if(first == 0)
{
return second;
}
if(second == 0)
{
return first;
}
// both numbers are equal
if(first == second)
{
return first;
}
// first is greater
else if(first > second)
{
return findGCD(first – second, second);
}
return findGCD(first, second – first);
// 0 is divisible by every number
}