C# Program to Find and Display the H.C.F of a Given Number

This C# Program Finds and Display the H.C.F of a Given Number. In other words the H.C.F is the largest of all the common factors.

Here is source code of the C# Program to Find and Display the H.C.F of a Given Number. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*

* C# Program to Find and Display the H.C.F of a Given Number

*/

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespace Program

{

class Program

{

publicstaticvoid Main(string[] args)

{

int num1,num2,i;

int hcf =0;

Console.Write("\nEnter the First Number : ");

num1 =int.Parse(Console.ReadLine());

Console.Write("\nEnter the Second Number : ");

num2 =int.Parse(Console.ReadLine());

for(i=1;i<=num1||i<=num2;++i)

{

if(num1%i==0&& num2%i==0)

{

hcf=i;

}

}

Console.Write("\nCommon Factor is : ");

Console.WriteLine(hcf);

Console.Read();

}

}

}

Here is the output of the C# Program:

advertisement

Enter the First Number : 12
Enter the Second Number : 16
Common Factor is : 4