Introduction
In this article I describe how to determine the Highest Common Factor (HCF) using mathematical rules. I think everyone understands the concept of HCF in mathematics. In this article I will determine the HCF of two numbers in a programing language. Before going in depth, let us first instead ensure that the concept of HCF is understood.
HCF is also called a Greatest Common Factor that is determined by two methods in math, they are:
- Repeated Division method
- Prime Factorization method
For example:
24 = 2* 2*2*3
60=2*2*3*5
Ans= 2*2*3=12
Now find the HCF through programing.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace gcd_finder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int num = int.Parse(textBox1.Text);
int num1 = int.Parse(textBox2.Text);
int gcd = findgcd(num, num1);
label1.Text = gcd.ToString();
}
int findgcd(int x, int y)
{
while (x != y)
{
if (x > y)
{
return findgcd(x - y, y);
}
else
{
return findgcd(x, y - x);
}
}
return x;
}
}
}
Output