0
4.3kviews
Explain recursive function. Write a program to find the GCD of a number by using recursive function.
1 Answer
0
340views

Recursive function:

In C, a function can call itself. This process is known as recursion. And a function that calls itself is called as the recursive function.In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.

Program:

#include <stdio.h>

#include <conio.h>

int gcd(int n1, int n2);

int main()

{

    int n1, n2;

    printf("Enter two positive integers: \n");

    scanf("%d %d", &n1, &n2);

    g= gcd(n1,n2);

    printf("G.C.D of %d and %d is %d.", n1, n2, g);

    return 0;

}

int gcd(int n1, int n2)

{

    while (n1 != n2)

    {

        if (n1 > n2)

        return gcd(n1 –n2, n2);

        else

        return gcd(n1, n2 –n1);

    }

    return a;

}

Output:

Enter two positive integers: 

366

60

G.C.D of 366 and 60 is 6.
Please log in to add an answer.