Program to find HCF iteratively in C++


In this tutorial, we will be discussing a program to find HCF iteratively.

For this, we will be provided with two numbers. Our task is to calculate the HCF of the given numbers using an iterative function.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int get_HCF(int a, int b){
   while (a != b){
      if (a > b)
         a = a - b;
      else
         b = b - a;
   }
   return a;
}
int main(){
   int a = 60, b = 96;
   cout << get_HCF(a, b) << endl;
   return 0;
}

Output

12

Updated on: 04-May-2020

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements