Product of first N factorials in C# -Learn how to find the factorial of first N  Numbers and product of result of factorials.

Product of first N factorials in C#

To find product of first N factorial numbers, first we have to find factorial of each number from 1 to N and then multiply the result of first N factorials.

For example to find product of first 5 factorial numbers,

 factorial of   1=1
  factorial of  2=2*1=2
  factorial of  3=3*2*1=6
  factorial of  4=4*3*2*1=24
  factorial of  5= 5*4*3*2*1=120

Now Product of first 5 factorial results 
as shown above is = 1*2*6*24*120   =  34560

Example Program First N factorials using function

class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("Product of first
            5 natural Numbers = "+p.findProduct(5));  
        }
        int findProduct(int n)
        {
            int j = 1;
            for (int i = n; i >= 1;i-- )
            {
               j=j* findFact(i);
            }

                return j;
        }
        int findFact(int f)
        {
            int j = 1;
            if(f==1 || f==2)
            {
                return f;
            }
            else
            {
                for(int i=2;i<=f;i++)
                {
                    j = j * i;
                }
                return j;
            }          
        }
    }

OUTPUT

Product of firs 5 natural Numbers = 34560

Example Program First N factorials using recursive function.

 class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            Console.WriteLine("Product of first 5 
            natural Numbers = "+p.findProduct(5)); 
        }
        int findProduct(int n)
        {
            int j = 1;
            for (int i = n; i >= 1;i-- )
            {
               j=j* findFact(i);
            }

                return j;
        }
        int findFact(int f)
        {
            int j = 1;
            if(f==1 || f==2)
            {
                return f;
            }
            else
            {
                return f*findFact(f - 1);
            }

            
        }
    }

OUTPUT

Product of first 5 natural Numbers = 34560

Related Posts