Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Factorial
- {
- class Program
- {
- static void Main(string[] args)
- {
- int val;
- while (!int.TryParse(Console.ReadLine(), out val))
- {
- Console.WriteLine("Enter number: 2-9");
- }
- ShowFactoria(fromNum: val); // this will do what you asked @abdul :)
- /*
- int factorial;
- while (!int.TryParse(Console.ReadLine(), out factorial))
- {
- Console.WriteLine("Invalid number entered, make sure you entered a n: 0-10");
- }
- int result = 0;
- bool hit = true;
- for (int i = factorial - 1; i > 0; i--)
- {
- if (hit)
- {
- result = factorial;
- hit = false;
- }
- result *= i;
- }
- Console.WriteLine(result);
- */
- Console.ReadLine();
- }
- private static void ShowFactoria(int fromNum, int to = 0)
- {
- if (fromNum < to)
- {
- Console.WriteLine("from can't be less than to!!!");
- return;
- }
- if (to < -1)
- {
- Console.WriteLine("to can't be less than 0");
- }
- // You can do some other check here...
- long temp = 0;
- bool hit = true;
- long result = 0;
- // 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
- for (int i = fromNum; i > to; i--)
- {
- if (hit)
- {
- temp = i * (i - 1);
- hit = !hit;
- }
- else
- {
- temp *= (i - 1);
- }
- if (i - 1 > 1)
- {
- Console.WriteLine(" + {0}", temp);
- result += temp;
- }
- //Console.WriteLine(string.Format("{0} * "))
- }
- Console.WriteLine("The addition of the !{0} shriek is: {1}", fromNum, result);
- Console.ReadLine();
- }
- }
- }
- By: @ivandrofly
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement