Sunday 28 September 2014

merge two array in one array in C#, Factorial in C#

 // make an array of strings. Note that we have included spaces
            string[] H1 = { "hello ", "and ", "welcome ", "to ", "this ", "demo! " };

            // put all the strings together
            Console.WriteLine(string.Concat(H1));

            // sort the strings, and put them together
            Array.Sort(H1);
            Console.WriteLine(string.Concat(H1));
            //*****************************************************//
            //In C# 3.0 you can use LINQ to accomplish this easily:

            int[] front = { 1, 2, 3, 4 };
            int[] back = { 5, 6, 7, 8 };
            int[] combined = front.Concat(back).ToArray();

            //In C# 2.0 you don't have such a direct way, but Array.Copy is probably the best solution:

            int[] front1 = { 1, 2, 3, 4 };
            int[] back1 = { 5, 6, 7, 8 };

            int[] combined1 = new int[front1.Length + back1.Length];
            Array.Copy(front1, combined1, front1.Length);
            Array.Copy(back1, 0, combined1, front1.Length, back1.Length);


            //Program that uses Array to Revers Word
            const string s2 = "This is Reverse word";
            string rev1 = Words.RWords(s2);
            Console.WriteLine(rev1);
         

            //----------Reverse String----------------------------

            string text = "HAMID ALI";
            StringBuilder reverse = new StringBuilder();

            for (int i = text.Length - 1; i >= 0; i--)
            {
                reverse.Append(text[i]);
            }
            Console.WriteLine(reverse);

            //----------------------Reverse string--------------------------------//
            string s = "HAMID ALI";
            char[] charArray = s.ToCharArray();
            Array.Reverse(charArray);
            Console.WriteLine(charArray);
            string s1 = charArray.ToString();
            Console.ReadKey();
            //-----------------------------------------------------//

            MatrixMultiplication MM = new MatrixMultiplication();
            MM.ReadMatrix();
            MM.MultiplyMatrix();
            MM.PrintMatrix();

            int[] ArrStr = new int[] { 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 3, 1, 4, 6, 7, 6, 4, 3 };
            Array.Sort(ArrStr);
            Array.Reverse(ArrStr);
            foreach (var str in ArrStr)
            {
                Console.WriteLine(str);
            }
            Console.ReadLine();


            //  clsA objA = new clsC();


            Console.WriteLine("Enter a number");
            int number1 = Convert.ToInt32(Console.ReadLine());
            long fact1 = GetFactorial(number1);
            Console.WriteLine("{0} factorial is {1}", number1, fact1);
            Console.ReadKey();








private static long GetFactorial(int number)
        {
            if (number == 0)
            {
                return 1;
            }
            return number * GetFactorial(number - 1);
        }








No comments:

Post a Comment