Составить программу нахождения НОД (НОК) элементов массива - C#
Формулировка задачи:
Составить программу нахождения НОД (НОК) элементов массива
апп
Решение задачи: «Составить программу нахождения НОД (НОК) элементов массива»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace НОД1
{
class Program
{
static int getNOD(int a, int b)
{
while (a != b)
if (a > b)
a -= b;
else
b -= a;
return a;
}
static int getCOMP(int[]a)
{
int comp = a[0];
for (int i=1; i<a.Length;i++)
{
comp = comp * a[i];
}
return comp;
}
static void Main(string[] args)
{
Console.WriteLine("Введите колличество чисел в массиве:");
int i = Convert.ToInt32(Console.ReadLine());
int[] arr = new int[i];
int[] arr1 = new int[i];
Console.WriteLine("-----------------");
Random rand = new Random();
for (int j = 0; j < i; j++)
{
arr[j] = rand.Next(1, 1000);
Console.Write("{0}__", arr[j]);
}
Console.WriteLine();
Console.WriteLine("-----------------");
Array.Sort(arr);
for (int j = 0; j < i; j++)
{
Console.Write("{0}__", arr[j]);
}
int nod = getNOD(arr[0], arr[1]);
for (int j = 2; j < arr.Length;j++)
{
nod=getNOD(nod,arr[j]);
}
long nok = getCOMP(arr) / nod;
Console.WriteLine();
Console.WriteLine("НОД равен "+nod);
Console.WriteLine("НОК равен "+nok);
Console.ReadKey();
}
}
}