Tuesday, August 25, 2009
Program to find HCF and LCM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static int hcf(int min, int max)
{
int rem;
if (min < max)
{
int temp = min;
min = max;
max = temp;
}
while (true)
{
rem = max % min;
if (rem == 0)
return min;
else
{
max = min;
min = rem;
}
}
}
static int lcm(int one, int two)
{
int lcm = one * two / hcf(one, two);
return lcm;
}
static void Main(string[] args)
{
int element, value, choice;
int[] array;
do
{
Console.WriteLine("1.HCF\n2.LCM\n3.exit\nchoice:");
choice = Convert.ToInt16(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("No. of elements:");
element = Convert.ToInt32(Console.ReadLine());
array = new int[element];
Console.WriteLine("Elements:\n");
for (int i = 0; i < element; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
value = array[0];
for (int i = 1; i < element; i++)
{
value = hcf(value , array[i]);
}
Console.WriteLine("HCF:" + value);
break;
case 2:
Console.WriteLine("No. of elements:");
element = Convert.ToInt32(Console.ReadLine());
array = new int[element];
Console.WriteLine("Elements:\n");
for (int i = 0; i < element; i++)
{
array[i] = Convert.ToInt32(Console.ReadLine());
}
value = array[0];
for (int i = 1; i < element; i++)
{
value = lcm(value, array[i]);
}
Console.WriteLine("LCM:" + value);
break;
case 3:
break;
default:
Console.WriteLine("\nwrong choice");
break;
}
} while (choice != 3) ;
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment