Write a C# Sharp program to get the Least Common Multiple (LCM) of more than two numbers. Take the numbers from a given array of positive integers
- برمجة سي شارب
- برمجة
- 2021-05-31
- ahmadghneem
الأجوبة
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
int[] nums1 = { 4, 6, 8 };
Console.WriteLine("Original array elements:");
for (int i = 0; i < nums1.Length; i++)
{
Console.Write(nums1[i] + " ");
}
Console.WriteLine("\nLCM of the numbers of the said array of positive integers: " + test(nums1));
int[] nums2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine("\nOriginal array elements:");
for (int i = 0; i < nums2.Length; i++)
{
Console.Write(nums2[i] + " ");
}
Console.WriteLine("\nLCM of the numbers of the said array of positive integers: " + test(nums2));
int[] nums3 = { 48, 72, 108 };
Console.WriteLine("\nOriginal array elements:");
for (int i = 0; i < nums3.Length; i++)
{
Console.Write(nums3[i] + " ");
}
Console.WriteLine("\nLCM of the numbers of the said array of positive integers: " + test(nums3));
}
static int gcd(int n1, int n2)
{
if (n2 == 0)
{
return n1;
}
else
{
return gcd(n2, n1 % n2);
}
}
public static int test(int[] numbers)
{
return numbers.Aggregate((S, val) => S * val / gcd(S, val));
}
}
}
أسئلة مشابهة
القوائم الدراسية التي ينتمي لها السؤال