Advertisement
ToDiR0S

CalculateSum

Aug 16th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.63 KB | None | 0 0
  1. // 05. Write a program that, for a given two integer numbers n and x, calculates the sum S = 1 + 1!/x + 2!/x2 + … + n!/xn.
  2. // Use only one loop.
  3. // Print the result with 5 digits after the decimal point.
  4. using System;
  5.  
  6. class CalculateSum
  7. {
  8.     static void Main()
  9.     {
  10.         int n = int.Parse(Console.ReadLine());
  11.         int x = int.Parse(Console.ReadLine());
  12.         double sum = 1;
  13.         double factorial = 1;
  14.  
  15.         for (int i = 1; i <= n; i++)
  16.         {
  17.             factorial = factorial * i;
  18.             sum += factorial / Math.Pow(x, i);
  19.         }
  20.  
  21.         Console.WriteLine("{0:#.00000}", sum);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement