C/C++ Program to Find GCD of Two Numbers Using Recursion

C/C++ Program to Find GCD of Two Numbers Using Recursion

Series 0, 1, 1, 2, 3, 5, 8, 13, 21 . . . . . . . is a Fibonacci series. In Fibonacci series, each term is the sum of the two going before terms.

The C and C++ program for Fibonacci series utilizing recursion is given beneath.

C Program

#include<stdio.h>
 
int gcd(int n,int m)
{
    if((n>=m)&&((n%m)==0))
        return(m);
    else
        gcd(m,(n%m));
}
 
int main()
{
    int n,m,result;
    printf("Input the first integer number:");
    scanf("%d",&n);
    printf("Input the second integer number:");
    scanf("%d",&m);
    result=gcd(n,m);
    printf("nGCD of %d and %d is %d",n,m,result);
 
    return 0;
}

C++ Program

#include<iostream>
#include<cstdlib>
 
using namespace std;
 
int gcd(int n,int m)
{
    if((n>=m)&&((n%m)==0))
        return(m);
    else
        gcd(m,(n%m));
}
 
int main()
{
    int n,m,result;
    cout<<"Input the first integer number:";
    cin>>n;
    cout<<"Input the second integer number:";
    cin>>m;
    result=gcd(n,m);
    cout<<"nGCD of "<<n<<" and "<<m<<" is "<<result;
 
    return 0;
}

Output

Leave a Comment

error: Alert: Content is protected!!