Q:

Write C++ program to find HCF of two numbers using recursion

0

Write C++ program to find HCF of two numbers using recursion

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.

#include <iostream>
using namespace std;
 
int Findgcd(int x, int y); // Function declaration
 
int main()
{
    int num1, num2, hcf;
 
    // Inputting 2 numbers from user
    cout<<"Enter any 2 numbers to find HCF or GCD: "<<endl;
    cin>>num1;
    cin>>num2;
 
    hcf = Findgcd(num1, num2);
 
    //printf("GCD of %d and %d = %d", num1, num2, hcf);
 
    cout<<"GCD of "<<num1 <<" and "<<num2 <<" = "<<hcf;
    return 0;
}
 
int Findgcd(int x, int y)
{
    if(y == 0)
        return x;
    else
        return Findgcd(y, x%y);
}

Result:

Enter any 2 numbers to find HCF or GCD: 

20

50

GCD of 20 and 50 = 10

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write C++ program to print elements of array using... >>
<< Write C++ program to find LCM of two numbers using...