Finding GCD/HCF of two integer numbers
Program to find Highest Common Factor (HCF), also called the Greatest Common Divisor between the two numbers entered by the user.
Learning Objectives
- Finding HCF/GCD of two numbers by successive subtraction of smaller number from the larger one until they are equal.
Source Code
|
|
Related ( ? ) : |
Run Output
Code Understanding
int x,y; cout<<“Enter two numbers separated by space: “; cin>>x>>y;
Two numbers are collected from the user.
while(x != y) //This loop will exit until x and y are both same
{
if(x>y) x=x-y; else y=y-x;
This is successive iteration of x and y in a way that x and y keep reducing until they are equal
}
cout<<“GCD/HCF = “<< x;
Here we print the result
Suggested Filename(s): hcf-gcd.cpp
Created: 20-Jan-2018 23:28 | Updated: 21-Jan-2018 00:05|
