Venciity

[ Telerik C#] LOOPS - EX 6

Feb 9th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. //Write a program that, for a given two integer numbers N and X, calculates the sum S = 1 + 1!/X + 2!/X^2 + … + N!/X^N
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace ex6
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Console.Write("Enter N: ");
  16.             double n = double.Parse(Console.ReadLine());
  17.             Console.Write("Enter X: ");
  18.             double x = double.Parse(Console.ReadLine());
  19.  
  20.             double numerator = 1;
  21.             double denominator = 1;
  22.             double singleResult = 0;
  23.             double totalResult = 0;
  24.  
  25.             for (double i = n; i >= 1; i--)
  26.             {
  27.                 for (double j = 1; j <= i; j++)
  28.                 {
  29.                     numerator = numerator * j;
  30.                     denominator = denominator * x;
  31.                 }
  32.                 singleResult = numerator / denominator;
  33.                 Console.WriteLine("{0}!/{1}^{0} = {2} / {3} = {4}", i, x, numerator, denominator, singleResult);
  34.                 numerator = 1;
  35.                 denominator = 1;
  36.                 totalResult += singleResult;
  37.             }
  38.             Console.WriteLine("-----------");
  39.             Console.WriteLine("Total Sum: {0}", totalResult + 1);
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment