Advertisement
kuruku

CalculateEquation

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