# Recursive program to print formula for GCD of n integers

Given a function gcd(a, b) to find GCD (Greatest Common Divisor) of two number. It is also known that GCD of three elements can be found by gcd(a, gcd(b, c)), similarly for four element it can find the GCD by gcd(a, gcd(b, gcd(c, d))). Given a positive integer n. The task is to print the formula to find the GCD of n integer using given gcd() function.

Examples:

```Input : n = 3
Output : gcd(int, gcd(int, int))

Input : n = 5
Output : gcd(int, gcd(int, gcd(int, gcd(int, int))))
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to use recursion to print the single line command. Now, to write a recursive function, say recursiveFun(n), the required string is composed of gcd(int, + recursiveFun(n – 1) + ). This means that the recursiveFun(n) should return a string that contains a call to itself and in order to evaluate that value, the recursive function will begin again for n – 1. This will, in turn, return another string with a call to n – 1 and so until n == 1 and the recursive function instead returns the string “int”.

Below is implementation of the above approach:

## C++

 `// CPP Program to print single line command ` `// to find the GCD of n integers ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print single line command ` `// to find GCD of n elements. ` `string recursiveFun(``int` `n) ` `{ ` `    ``// base case ` `    ``if` `(n == 1) ` `        ``return` `"int"``; ` ` `  `    ``// Recursive Step ` `    ``return` `"gcd(int, "` `+ recursiveFun(n - 1) + ``")"``; ` `} ` ` `  `// Driver Program ` `int` `main() ` `{ ` `    ``int` `n = 5; ` ` `  `    ``cout << recursiveFun(n) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java Program to print  ` `// single line command to ` `// find the GCD of n integers ` `class` `GFG ` `{ ` `     `  `// Function to print single  ` `// line command to find GCD  ` `// of n elements. ` `static` `String recursiveFun(``int` `n) ` `{ ` `    ``// base case ` `    ``if` `(n == ``1``) ` `        ``return` `"int"``; ` ` `  `    ``// Recursive Step ` `    ``return` `"gcd(int, "` `+  ` `            ``recursiveFun(n - ``1``) + ``")"``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String [] arg) ` `{ ` `    ``int` `n = ``5``; ` ` `  `    ``System.out.println(recursiveFun(n)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Smitha `

## Python3

 `# Python 3 Program to print single line  ` `# command to find the GCD of n integers  ` ` `  `# Function to print single line command  ` `# to find GCD of n elements.  ` `def` `recursiveFun(n):  ` `     `  `    ``# base case  ` `    ``if` `(n ``=``=` `1``):  ` `        ``return` `"int"` ` `  `    ``# Recursive Step  ` `    ``return` `"gcd(int, "` `+` `recursiveFun(n ``-` `1``) ``+` `")"` ` `  `# Driver Code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `5` `    ``print``(recursiveFun(n))  ` ` `  `# This code is contributed  ` `# by PrinciRaj1992 `

## C#

 `// C# Program to print single ` `// line command to find the ` `// GCD of n integers ` `using` `System; ` `class` `GFG ` `{ ` `     `  `// Function to print single  ` `// line command to find GCD  ` `// of n elements. ` `static` `String recursiveFun(``int` `n) ` `{ ` `    ``// base case ` `    ``if` `(n == 1) ` `        ``return` `"int"``; ` ` `  `    ``// Recursive Step ` `    ``return` `"gcd(int, "` `+ ` `            ``recursiveFun(n - 1) + ``")"``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main() ` `{ ` `    ``int` `n = 5; ` ` `  `    ``Console.Write(recursiveFun(n)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by smitha `

Output:

```gcd(int, gcd(int, gcd(int, gcd(int, int))))
```

Time Complexity: O(N), where N is the given number.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.