Monday, March 28, 2016

factorial number

  1st method:-

int i, number, fact;
            Console.WriteLine("Enter the Number");
            number = int.Parse(Console.ReadLine());
            fact = number;
            for (i = number - 1; i >= 1; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFactorial of Given Number is: " + fact);

2nd method:-
class Program
        {

            static int Fact(int n)
            {
                if (n <= 1)
                    return 1;
                return n * Fact(n - 1);
            }

            static int Factorial(int n)
            {
                if (n <= 1)
                    return 1;
                int result = 1;
                for (int i = 2; i <= n; i++)
                {
                    result = result * i;
                }
                return result;
            }


            static void Main(string[] args)
            {
                Console.Write("Enter a Number to find factorial: ");
                int n = Convert.ToInt32(Console.ReadLine());
                int r = Fact(n);
                Console.WriteLine(n.ToString() + "! = " + r.ToString());

                Console.Write("Enter a Number to find factorial: ");
                n = Convert.ToInt32(Console.ReadLine());
                r = Factorial(n);
                Console.WriteLine(n.ToString() + "! = " + r.ToString());
            }
        }

3rd Method:-
  class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Enter a number");
                int number = Convert.ToInt32(Console.ReadLine());
                long fact = GetFactorial(number);
                Console.WriteLine(fact);
                Console.ReadKey();
            }
            private static long GetFactorial(int no)
            {
                if (no == 0)
                {
                    return 1;
                }
                else
                {
                    return no * GetFactorial(no - 1);
                }
            }

        }

2nd heighest value in array

            int[] myArray = new int[] { 0, 1, 2, 3, 13, 8, 5 };
            int largest = int.MinValue;
            int second = int.MinValue;
            foreach (int i in myArray)
            {
                if (i > largest)
                {
                    second = largest;
                    largest = i;
                }
                else if (i > second)
                    second = i;
            }

            System.Console.WriteLine(second);
            int secondHighest = (from number in myArray
                                 orderby number descending
                                 select number).Distinct().Skip(1).First();

            System.Console.WriteLine(secondHighest);

max value in array

           int[] anArray = { 2, 5, 10, 9, 10, 6 };

            int? maxVal = null; //nullable so this works even if you have all super-low negatives
            int index = -1;
            for (int i = 0; i < anArray.Length; i++)
            {
                int thisNum = anArray[i];
                if (!maxVal.HasValue || thisNum > maxVal.Value)
                {
                    maxVal = thisNum;
                    index = i;
                }
            }
            Console.WriteLine(maxVal);

swap two number without temp variable

int a=10,b=30;
a=a+b;
b=a-b;
a=a-b;

Find the value from array when age is more than 30

 const data = [   { id: 1, name: 'Alice', age: 25 },   { id: 2, name: 'Bob', age: 30 },   { id: 3, name: 'Charlie', ...