GCD USING WHILE LOOP IN C++

Find GCD using while loop

  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. int n1, n2;
  6. cout << "Enter two numbers: ";
  7. cin >> n1 >> n2;
  8. while(n1 != n2)
  9. {
  10. if(n1 > n2)
  11. n1 -= n2;
  12. else
  13. n2 -= n1;
  14. }
  15. cout << "HCF = " << n1;
  16. return 0;
  17. }
Output
Enter two numbers: 78
52
HCF = 26

Comments

Post a Comment

Popular posts from this blog

Scan Converting a sStraight Line

DDA and Bresenham's Algorithm

priority&non priority scheduling