Q:

Write C# program to find HCF of any two numbers

0

Write C# program to find HCF of any two numbers

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
public class csharpExercise
{
    static void Main(string[] args)
    {
        int i, num1, num2, min, HCF = 1;
 
        // Reading 2 numbers from user
        Console.WriteLine("Enter any number to print in words: ");
        num1 = Convert.ToInt32(Console.ReadLine());
        num2 = Convert.ToInt32(Console.ReadLine());
 
 
        // Find min number between two numbers
        min = (num1 < num2) ? num1 : num2;
 
        for (i = 1; i <= min; i++)
        {
            if (num1 % i == 0 && num2 % i == 0)
            {
                HCF = i;
            }
        }
 
        Console.WriteLine("HCF of " + num1 + " and " + num2 + " is: " + HCF);
 
        Console.ReadLine();
    }
}

Result:

Enter any number to print in words: 

54

24

HCF of 54 and 24 is: 6

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Write C# program to find LCM of any two numbers... >>
<< Write C# program to print number in words...