wingman007

Loops

Nov 28th, 2015
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.29 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class LoopsDemos
  5. {
  6.     static void Main()
  7.     {
  8.         /*
  9.         //int n = int.Parse(Console.ReadLine());
  10.         //int i = 0; // initialization
  11.         //while (i < n) // condition
  12.         //{
  13.         //    Console.WriteLine(i);
  14.         //    i++; // update
  15.         //}
  16.         */
  17.         /*
  18.         int n = int.Parse(Console.ReadLine());
  19.         int i = 0; // initialization
  20.         int sum = 0;
  21.         while (i <= n) // condition
  22.         {
  23.             sum += i;
  24.             i++; // update
  25.         }
  26.         Console.WriteLine(sum);
  27.         */
  28.         /*
  29.         int n = int.Parse(Console.ReadLine());
  30.         // int i = 0; // initialization
  31.         // int sum = 0;
  32.         BigInteger sum = 0;
  33.         while (n > 0) // condition
  34.         {
  35.             checked
  36.             {
  37.                 sum += n;
  38.             }          
  39.             n--; // update
  40.         }
  41.         Console.WriteLine(sum);
  42.         */
  43.         /*
  44.         int n = int.Parse(Console.ReadLine());
  45.         int i = 2;
  46.         bool isprime = true;
  47.         while (i < n)
  48.         {
  49.             if (n % i == 0)
  50.             {
  51.                 isprime = false;
  52.                 break;
  53.             }
  54.  
  55.             i++;
  56.         }
  57.         Console.WriteLine("the number {0} is prime: {1}", n, isprime);
  58.         */
  59.         /*
  60.         int n = int.Parse(Console.ReadLine());
  61.         int i = 2;
  62.         int maxDivider = (int)Math.Sqrt(n);
  63.         bool isPrime = true;
  64.         while (i <= maxDivider)
  65.         {
  66.             if (n % i == 0)
  67.             {
  68.                 isPrime = false;
  69.                 break;
  70.             }
  71.  
  72.             i++;
  73.         }
  74.         Console.WriteLine("The number {0} is prime: {1}", n, isPrime);
  75.         */
  76.         /*
  77.         int n = int.Parse(Console.ReadLine());
  78.         int i = 2;
  79.         int maxDivider = (int)Math.Sqrt(n);
  80.         bool isPrime = true;
  81.         while (isPrime && i <= maxDivider)
  82.         {
  83.             if (n % i == 0)
  84.             {
  85.                 isPrime = false;
  86.                 // break;
  87.             }
  88.  
  89.             i++;
  90.         }
  91.         Console.WriteLine("The number {0} is prime: {1}", n, isPrime);
  92.         */
  93.  
  94.         /*
  95.         Random rand = new Random();
  96.         int n = rand.Next(1, 10);
  97.         int i = 0;
  98.         while (i < n)
  99.         {
  100.             Console.WriteLine("i = {0} n = {1}", i, n);
  101.             i++;
  102.         }
  103.         */
  104.  
  105.         /*
  106.         // factorial 0!= 1 n! = (n-1)!*n;
  107.         int n = int.Parse(Console.ReadLine());
  108.         int factorial = 1;
  109.         while (n > 0)
  110.         {
  111.             checked
  112.             {
  113.                 factorial *= n--;
  114.             }
  115.            
  116.         }
  117.         Console.WriteLine(factorial);
  118.         */
  119.         /*
  120.         int n = int.Parse(Console.ReadLine());
  121.         BigInteger factorial = 1;
  122.         while (n > 0)
  123.         {
  124.             checked
  125.             {
  126.                 factorial *= n--;
  127.             }
  128.  
  129.         }
  130.         Console.WriteLine(factorial);
  131.         */
  132.         /* This is not working factorial 0
  133.         int n = int.Parse(Console.ReadLine());
  134.         BigInteger factorial = 1;
  135.         do
  136.         {
  137.             checked
  138.             {
  139.                 factorial *= n--;
  140.             }
  141.  
  142.         } while (n > 0);
  143.  
  144.         Console.WriteLine(factorial);
  145.         */
  146.         /*
  147.         string numberAsString = Console.ReadLine();
  148.         int n = Convert.ToInt32(numberAsString);
  149.         int factorial = 1;
  150.         if (n != 0)
  151.         {
  152.             do
  153.             {
  154.                 factorial *= n;
  155.                 n--;
  156.             } while (n > 0);
  157.  
  158.         }
  159.         Console.WriteLine("n! = " + factorial);
  160.         */
  161.         /*
  162.         ConsoleKeyInfo cki;
  163.         cki = Console.ReadKey();
  164.         Console.WriteLine(cki.Key == ConsoleKey.RightArrow);
  165.         int positionX = 0;
  166.         int positionY = 0;
  167.  
  168.         do {
  169.             while (!Console.KeyAvailable)
  170.             {
  171.                 // Console.WriteLine("Cycle trough your live");
  172.                 Console.Clear();
  173.                 Console.SetCursorPosition(positionX, positionY);
  174.                 Console.Write("*");
  175.                 System.Threading.Thread.Sleep(100);
  176.             }
  177.             cki = Console.ReadKey(true);
  178.             switch (cki.Key)
  179.             {
  180.                 case ConsoleKey.RightArrow:
  181.                     positionX++;
  182.                     break;
  183.                 case ConsoleKey.LeftArrow:
  184.                     positionX--;
  185.                     break;
  186.                 case ConsoleKey.DownArrow:
  187.                     positionY++;
  188.                     break;
  189.                 case ConsoleKey.UpArrow:
  190.                     positionY--;
  191.                     break;
  192.             }
  193.  
  194.         } while (cki.Key != ConsoleKey.Escape);
  195.         */
  196.         /*
  197.         int n = 0;
  198.        
  199.         do
  200.         {
  201.             Console.Write("Please enter a positive number: ");
  202.             n = int.Parse(Console.ReadLine());
  203.         } while(n < 0);
  204.         */
  205.         /*
  206.         int n = int.Parse(Console.ReadLine());
  207.         int m = int.Parse(Console.ReadLine());
  208.         int number = n;
  209.         decimal product = 1;
  210.         do
  211.         {
  212.             product *= number;
  213.             number++;
  214.         }
  215.         while (number <= m);
  216.         Console.WriteLine("product[{0}..{1}] = {2}", n, m, product);
  217.         */
  218.         /*
  219.         int i = 0;
  220.         while (i < 10)
  221.         {
  222.             Console.WriteLine(i);
  223.             i++;
  224.         }
  225.         */
  226.         /*
  227.         for (int i = 0; i < 10; i++)
  228.         {
  229.             Console.WriteLine(i);
  230.         }
  231.         */
  232.         /*
  233.         int sum = 0;
  234.         for (int i = 0; i <= 5; i++)
  235.         {
  236.             sum += i;
  237.         }
  238.         Console.WriteLine(sum);
  239.         */
  240.         /*
  241.         int factorial = 1;
  242.         for (int i = 1; i <= 5; i++)
  243.         {
  244.             factorial *= i;
  245.         }
  246.         Console.WriteLine(factorial);
  247.         */
  248.         /*
  249.         Random rand = new Random();
  250.         int n = rand.Next(1, 10);
  251.  
  252.         for (;;)
  253.         {
  254.             Console.WriteLine("cycle");
  255.         }
  256.         */
  257.         /*
  258.         for (int i = 1, sum = 1; i <= 128; i = i * 2, sum += i)
  259.         {
  260.             Console.WriteLine("i={0}, sum={1}", i, sum);
  261.         }
  262.         */
  263.  
  264.         /*
  265.         int n = int.Parse(Console.ReadLine());
  266.         int m = int.Parse(Console.ReadLine());
  267.         decimal result = 1;
  268.         for (int i = 0; i < m; i++)
  269.         {
  270.             result *= n;
  271.         }
  272.         Console.WriteLine("n^m = " + result);
  273.         */
  274.         /*
  275.         int n = int.Parse(Console.ReadLine());
  276.         int sum = 0;
  277.         for (int i = 1; i <= n; i += 2)
  278.         {
  279.             if (i % 7 == 0)
  280.             {
  281.                 continue;
  282.             }
  283.             sum += i;
  284.         }
  285.         Console.WriteLine("sum = {0}", sum);
  286.         */
  287.         /*
  288.         string[] group = { "Stoyan", "Vasko", "Petko", "Ivan" };
  289.  
  290.         // foreach (string name in group)
  291.         foreach (var name in group)
  292.         {
  293.             Console.WriteLine(name);
  294.         }
  295.         */
  296.         /*
  297.         string[] days = {
  298.    "Monday", "Tuesday", "Wednesday", "Thursday",
  299.    "Friday", "Saturday", "Sunday" };
  300.         foreach (var day in days)
  301.         {
  302.             Console.WriteLine(day);
  303.         }
  304.         */
  305.         /*
  306.         int n = int.Parse(Console.ReadLine());
  307.  
  308.         for (int i = 0; i < n; i++)
  309.         {
  310.             for (int j = 0; j < i; j++)
  311.             {
  312.                 Console.Write(j);
  313.             }
  314.             Console.WriteLine();
  315.         }
  316.         */
  317.         /*
  318.         int n = int.Parse(Console.ReadLine());
  319.         int m = int.Parse(Console.ReadLine());
  320.         for (int number = n; number <= m; number++)
  321.         {
  322.             bool prime = true;
  323.             int divider = 2;
  324.             int maxDivider = (int)Math.Sqrt(number);
  325.             while (divider <= maxDivider)
  326.             {
  327.                 if (number % divider == 0)
  328.                 {
  329.                     prime = false;
  330.                     break;
  331.                 }
  332.                 divider++;
  333.             }
  334.             if (prime)
  335.                 Console.Write("{0} ", number);
  336.         }
  337.         */
  338.         /*
  339.         int sum = 0;
  340.         int i = 0;
  341.         while(i < 100)
  342.         {
  343.             Console.WriteLine(i);
  344.             if (i % 2 == 0)
  345.             {
  346.                 i++;
  347.                 continue;  
  348.             }
  349.                
  350.             sum += i;
  351.             i++;
  352.         }
  353.         */
  354.         /*
  355.         breakIn:;
  356.  
  357.         int outerCounter = 0;
  358.         for (int outer = 0; outer < 10; outer++)
  359.         {
  360.             for (int inner = 0; inner < 10; inner++)
  361.             {
  362.                 if (inner % 3 == 0)
  363.                     continue; if (outer == 7)
  364.                     break; if (inner + outer > 9)
  365.                     goto breakOut;
  366.             }
  367.             outerCounter++;
  368.         }
  369.         breakOut:
  370.         goto breakIn;
  371.         */
  372.         /*
  373.         for (int a = 1; a <= 9; a++)
  374.             for (int b = 0; b <= 9; b++)
  375.                 for (int c = 0; c <= 9; c++)
  376.                     for (int d = 0; d <= 9; d++)
  377.                         if (a + b == c + d)
  378.                             Console.WriteLine("{0}{1}{2}{3}", a, b, c, d);
  379.         */
  380.         /*
  381.         int counter = 0;
  382.         int i1, i2, i3, i4, i5, i6;
  383.         for (i1 = 1; i1 <= 44; i1++)
  384.             for (i2 = i1 + 1; i2 <= 45; i2++)
  385.                 for (i3 = i2 + 1; i3 <= 46; i3++)
  386.                     for (i4 = i3 + 1; i4 <= 47; i4++)
  387.                         for (i5 = i4 + 1; i5 <= 48; i5++)
  388.                             for (i6 = i5 + 1; i6 <= 49; i6++)
  389.                             {
  390.                                 // Console.WriteLine("{0} {1} {2} {3} {4} {5}", i1, i2, i3, i4, i5, i6);
  391.                                 // Try to comment the above row to speed up the program
  392.                                 counter++;
  393.                             }
  394.         Console.WriteLine("{0} combinations", counter);
  395.         */
  396.         // Console.ForegroundColor
  397.  
  398.     }
  399.  
  400.     static int factorial(int n)
  401.     {
  402.         int factorial = 1;
  403.         while (n > 0)
  404.         {
  405.             checked
  406.             {
  407.                 factorial *= n--;
  408.             }
  409.  
  410.         }
  411.         return factorial;
  412.     }
  413. }
Advertisement
Add Comment
Please, Sign In to add comment