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


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


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Introduction

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


  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int Findgcd(int x, int y); // Function declaration
  5.  
  6. int main()
  7. {
  8. int num1, num2, hcf;
  9.  
  10. // Inputting 2 numbers from user
  11. cout<<"Enter any 2 numbers to find HCF or GCD: "<<endl;
  12. cin>>num1;
  13. cin>>num2;
  14.  
  15. hcf = Findgcd(num1, num2);
  16.  
  17. //printf("GCD of %d and %d = %d", num1, num2, hcf);
  18.  
  19. cout<<"GCD of "<<num1 <<" and "<<num2 <<" = "<<hcf;
  20. return 0;
  21. }
  22.  
  23. int Findgcd(int x, int y)
  24. {
  25. if(y == 0)
  26. return x;
  27. else
  28. return Findgcd(y, x%y);
  29. }

Result

C++ language techstudy.org

     





Related Topics




Popular Posts