Home » .Net » C# programs

# C# program to find the HCF of two given numbers

Here, we are going to learn **how to find the HCF of two given numbers in C#?**

Submitted by Nidhi, on September 21, 2020

Here we will enter two integer numbers from the keyboard and then find the highest common factor of two numbers.

**For example:**

Numbers: 9, 15 Factors of 9 are: 1, 3, 9 Factors of 15 are: 1, 3, 5, 15 Then the Highest Common Factor will be 3.

**Program:**

The source code to find the HCF of two numbers is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the HCF of two given numbers. using System; class HcfClass { static int GetHcf(int number1, int number2) { int iLoop = 1; int hcf = 0; while (iLoop <= number1 || iLoop <= number2) { if (number1 % iLoop == 0 && number2 % iLoop == 0) hcf = iLoop; iLoop++; } return hcf; } static void Main(string[] args) { int number1=0; int number2=0; int hcf = 0; Console.Write("Enter the First Number : "); number1 = int.Parse(Console.ReadLine()); Console.Write("Enter the Second Number : "); number2 = int.Parse(Console.ReadLine()); hcf = GetHcf(number1, number2); Console.Write("\nHighest Common Factor is : "); Console.WriteLine(hcf); } }

**Output:**

Enter the First Number : 15 Enter the Second Number : 9 Highest Common Factor is : 3 Press any key to continue . . .

**Explanation:**

Here, we created a class *HcfClass* that contains two methods *GetHcf()* and *Main()*. In the *GetHcf()*, we find the highest common factor of two numbers.

while (iLoop <= number1 || iLoop <= number2) { if (number1 % iLoop == 0 && number2 % iLoop == 0) hcf = iLoop; iLoop++; }

In the above code, we checked the common factor of both numbers, the loop executed till the value of counter variable *iLoop* is less than and equal to anyone of the given number and update the value of the common factor. That's why we loop ends then we have the highest common factor. The *GetHcf()* method returns the HCF to the calling method.

In the *Main()* method, we read the values of two integer numbers and then calculated the HCF. Then the HCF is printed on the console screen.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.