Write a C# Sharp program to sort a given positive number in descending/ascending order

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to sort a given positive number in descending/ascending order. 
Descending -> Highest to lowest.
Ascending -> Lowest to highest.
Expected Output:
Original Number: 134543
Descending order of the said number: 544331
Ascending order of the said number: 133445
Original Number: 4375973
Descending order of the said number: 9775433
Ascending order of the said number: 3345779

الأجوبة

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = 134543;
            Console.WriteLine("Original Number: " + n);
            Console.WriteLine("\nDescending order of the said number: " + test_dsc(n));
            Console.WriteLine("\nAscending order of the said number: " + test_asc(n));
            n = 4375973;
            Console.WriteLine("\nOriginal Number: " + n);
            Console.WriteLine("\nDescending order of the said number: " + test_dsc(n));
            Console.WriteLine("\nAscending order of the said number: " + test_asc(n));
        }
        public static int test_dsc(int num)
        {
            return Convert.ToInt32(String.Concat(num.ToString().OrderByDescending(x => x)));
        }

        public static int test_asc(int num)
        {
            return Convert.ToInt32(String.Concat(num.ToString().OrderBy(x => x)));
        } 
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...