ellapt

6.6.SumFactOverPowOfX

Dec 4th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. using System;
  2. class SumFactOverPowOfX
  3. {
  4. static void Main()
  5. {
  6. string strNum;
  7. int n;
  8. int x;
  9. decimal fact = 1M;
  10. decimal powX = 1M;
  11. decimal sum = 1M;
  12. do
  13. {
  14. Console.Write("Please, enter an unsigned integer number n: ");
  15. }
  16. while (!int.TryParse(strNum = Console.ReadLine(), out n));
  17.  
  18. do
  19. {
  20. Console.Write("Please, enter an integer number x, not equal to 0: ");
  21. }
  22. while (!(int.TryParse(strNum = Console.ReadLine(), out x)) || x == 0);
  23.  
  24. if (n == 0)
  25. {
  26. sum += 1M / (decimal)x; // 0!=1
  27. }
  28. else
  29. {
  30. for (int i = 1; i <= n; i++)
  31. {
  32. fact *= (decimal)i;
  33. powX *= (decimal)x;
  34. sum += fact / powX;
  35. }
  36. }
  37. Console.WriteLine("For n={0}, x={1} the sum is: {2}", n, x, sum);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment