For a given two integer numbers N and X, calculates the sumS = 1 + 1!/X + 2!/X2 + ... + N!/XN - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Algorithm

Description

For a given two integer numbers N and X, calculates the sumS = 1 + 1!/X + 2!/X2 + ... + N!/XN

Demo Code


using System;/*from   ww  w.ja  v  a 2  s.  co m*/

class SumS
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        int x = int.Parse(Console.ReadLine());
        decimal resultTemp = 1;
        decimal NFactorial = 1;
        decimal sumS = 0;
        for (int i = 1; i <= n; i++)
        {
            NFactorial *= i;
            resultTemp *= x;
            sumS += (NFactorial / resultTemp);
        }
        Console.WriteLine(1 + sumS);
    }
}

Related Tutorials