In this post, you will learn ** how to Program to Find G.C.D Using Recursion in C++** programming language.

This lesson will teach you how to Program to Find G.C.D Using Recursion, with a recursion statement, mathematical operations and decision making statement using the C++ Language. Letâ€™s look at the below source code.

## How to Find G.C.D Using Recursion?

RUN CODE SNIPPET## Source Code

#include<iostream> using namespace std; int gcd(int a, int b) { if (a == 0 || b == 0) return 0; else if (a == b) return a; else if (a > b) return gcd(a-b, b); else return gcd(a, b-a); } int main() { int a, b; cin>>a>>b; cout<<"Enter two positive numbers: "<<a<<","<<b<<endl; cout<<"\nGCD of "<< a <<" and "<< b <<" is "<< gcd(a, b); return 0; }

## Input

63 42

## Output

Enter two positive numbers: 63,42 GCD of 63 and 42 is 21

The statementsÂ **#include<iostream>, using namespace std, int main**Â are the main factors that **support** the function of the source code. Now we can look into the working and layout of the codeâ€™s function.

- In mathematics, the greatest common divisor of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers. This is the
**Greatest Common Devisor â€“****GCD**of two numbers. - In this code we have two sections, in the first one we declare the function
`int gcd(int a, int b)`

to initiate the recursive function, where we declare the variables**a**and**b**as anÂ**integers.** - UsingÂ
**if else if ladderÂ**we create multiple else if functions and statements;`if (a == 0 || b == 0)`

Â the condition with the statement`return 0`

`else if (a == b)`

the condition with the statement`return a`

`else if (a > b)`

the condition with the statement`return gcd(a-b, b)`

`else return gcd(a, b-a)`

as the last statement in theÂ**if else if ladder.Â** - The if statement is executed and the condition is verified, if the condition is false it moves to the next else if statement and when a condition is satisfied the statement/function is executed.
- The function is repeated with the recursion and the GCD of the two numbers is found.
- The function then moves to the output section of the code, where we use output statements to display the answer stored in
`gcd(a, b)`

**Note:** The **â€˜ << endl â€˜ **in the code is used to **end the current line **and move to the next line and **â€˜\nâ€™ **is also a **new line function,** to understand how both the functions work exclude it from the code, move it around and work with it.