Advertisement
AnitaN

06.LoopsHomework/05.Calculate

Mar 29th, 2014
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. //Problem 5. Calculate 1 + 1!/X + 2!/X2 + … + N!/XN
  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. Use only one loop. Print the result with 5 digits after the decimal point.
  3.  
  4. using System;
  5. class Calculate
  6. {
  7.     static ulong FactorialCalc(uint number)
  8.     {
  9.         ulong sum = 1;
  10.         for (uint i = 1; i <= number; i++)
  11.         {
  12.             sum = sum * i;
  13.         }
  14.         return sum;
  15.     }
  16.  
  17.     static void Main()
  18.     {
  19.         Console.Write("Please, enter X:");
  20.         double x = int.Parse(Console.ReadLine());
  21.         Console.Write("Please, enter N:");
  22.         double n = int.Parse(Console.ReadLine());
  23.         double theSum = 1;
  24.         for (uint i = 1; i <= n; i++)
  25.         {
  26.             theSum = theSum + (FactorialCalc(i) / Math.Pow(x, i));
  27.         }
  28.         Console.WriteLine("S=1+1!/X+2!/X2+...+N!/XN = {0}",theSum);
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement