Program to find HCF iteratively in C++

C++Server Side ProgrammingProgramming

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
raja
Published on 04-May-2020 07:20:27
Advertisements