Write C++ program to find HCF of two numbers


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.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int i, num1, num2, min, HCF=1;
  8.  
  9. //Read two numbers from user
  10. cout<<"Enter any two numbers:";
  11. cin>>num1;
  12. cin>>num2;
  13.  
  14. // Find min number between two numbers
  15. min = (num1<num2) ? num1 : num2;
  16.  
  17. for(i=1; i<=min; i++)
  18. {
  19. if(num1%i==0 && num2%i==0)
  20. {
  21. HCF = i;
  22. }
  23. }
  24.  
  25. cout<<"HCF of "<<num1<< " and "<< num2<< " is: " <<HCF;
  26. return 0;
  27.  
  28. }
  29.  

Result

C++ language techstudy.org

     





Related Topics




Popular Posts