Программа получения в порядке убывания всех делителей данного числа - C#
Формулировка задачи:
Помогите, пожалуйста. Выводятся делители вводимого числа. Как их расположить в порядке убывания?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int x, a;
Console.Write("Введите число ");
x = Convert.ToInt32(Console.ReadLine());
for (a = 1; a <= x; a++)
{
if ((x % a) == 0)
Console.WriteLine("Делитель числа {0} : {1}", x, a);
}
Console.ReadKey();
}
}
}
Решение задачи: «Программа получения в порядке убывания всех делителей данного числа»
textual
Листинг программы
namespace ConsoleApplication10
{
class Program
{
static void Main(string[] args)
{
int x;
var list = new List<int>();
Console.WriteLine("введите число: ");
int.TryParse(Console.ReadLine(), out x);
for(var i = 1; i <= x; i++)
if (x%i == 0)
{
list.Add(i);
Console.WriteLine("Делитель числа {0} : {1}", x, i);
}
var f = (from i in list orderby i descending select i).ToArray();
Console.WriteLine("\nДелители в обр. порядке:");
Array.ForEach(f, i => Console.WriteLine("{0}", i));
Console.ReadKey();
}
}
}