Advertisement
Krissy

H6_Loops_T6_SumXN

Dec 8th, 2012
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4.  
  5. class SumXn
  6. {
  7.     static void Main()
  8.     {
  9.         Console.WriteLine("Please enter integer x:");
  10.         int x = int.Parse(Console.ReadLine());
  11.         Console.WriteLine("Please enter positive integer n:");
  12.         int n = int.Parse(Console.ReadLine());
  13.  
  14.         decimal productNFactorial = 1m;
  15.  
  16.         decimal xPowerN = 1m;
  17.  
  18.         decimal sumxN = 1m;
  19.         if (n >= 0) // factorial could be calculated from positive integer only. 0!=1
  20.         {
  21.             for (int i = 1; i <= n; i++)
  22.             {
  23.                 for (int j = 1; j <= i; j++)
  24.                 {
  25.                     productNFactorial = (productNFactorial * j);
  26.                 }
  27.  
  28.                 xPowerN = xPowerN * x;
  29.                 sumxN = sumxN + (productNFactorial / xPowerN);
  30.                 productNFactorial = 1m;
  31.             }
  32.  
  33.  
  34.             Console.WriteLine("The result is: {0}", sumxN);
  35.         }
  36.         else
  37.         {
  38.             Console.WriteLine("Invalid input!");
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement