Wednesday, 22 April 2015

How find the HCF of any given two number in c# asp.net ?


Here I written a C# Program to 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.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
          
            int num1,num2,i;
            int hcf =0;
            Console.Write("\n Please Enter the First Number : ");
            num1 = int.Parse(Console.ReadLine());
            Console.Write("\n Please Enter 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("\nThe Common Factor (HCF) is : ");
            Console.WriteLine(hcf);
            Console.Read();
        }
        
    }
}

Output :-



No comments:

Post a Comment