Advertisement
Guest User

FactorialFormula

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