Advertisement
n4wn4w

HOMEWORKS

Mar 19th, 2015
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 23.86 KB | None | 0 0
  1. Оператори и изрази///////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2.  
  3. using System;
  4. 2
  5.  5 problem IsThirdDigit7
  6.  
  7. 3 class IsThirdDigit7
  8. 4 {
  9. 5     //Write an expression that checks for given integer if its third digit from right-to-left is 7.  
  10. 6     static void Main(string[] args)
  11. 7     {
  12. 8         Console.WriteLine("Enter an integer number:");
  13. 9         int number = int.Parse(Console.ReadLine());
  14. 10         int pathToDigit = number / 100;
  15. 11         int thirdDigit = pathToDigit % 10;
  16. 12         bool isSeven = thirdDigit == 7;
  17. 13         Console.WriteLine("Is the third digit 7?\n" + isSeven);
  18. 14     }
  19. 15 }
  20.  
  21.  
  22. 7 point in a circle
  23.  
  24. using System;
  25. 2
  26.  
  27. 3 class PointInACircle
  28. 4 {
  29. 5     //Write an expression that checks if given point (x,  y) is inside a circle K({0, 0}, 2).
  30. 6     static void Main(string[] args)
  31. 7     {
  32. 8         Console.WriteLine("Enter coordinate x of the point");
  33. 9         decimal pointX = decimal.Parse(Console.ReadLine());
  34. 10         Console.WriteLine("Enter coordinate y of the point");
  35. 11         decimal pointY = decimal.Parse(Console.ReadLine());
  36. 12         byte radius = 2;
  37. 13         //Pythagorean Theorem a^2 + b^2 = c^2 ==> (x*x) + (y*y) <= radius * radius.
  38. 14         //Operator * is faster than Math.Pow()
  39. 15         bool isInCircle = pointX * pointX + pointY * pointY <= radius * radius;
  40. 16         Console.WriteLine("Is the given point in the circle?\n{0}", isInCircle);
  41. 17     }
  42. 18 }
  43.  
  44.  
  45. 8 prime number chek
  46.  
  47. using System;
  48. 2
  49.  
  50. 3 class PrimeNumberCheck
  51. 4 {
  52. 5     //Write an expression that checks if given positive integer number n (n ≤ 100) is prime (i.e. it is divisible without  
  53. 6     //remainder only to itself and 1).  
  54. 7     static void Main(string[] args)
  55. 8     {
  56. 9         Console.WriteLine("Enter positive integer number n between 2 and 100");
  57. 10         int numberN = int.Parse(Console.ReadLine());
  58. 11         while (numberN < 2 || numberN > 100)
  59. 12         {
  60. 13             Console.WriteLine("Try again");
  61. 14             numberN = int.Parse(Console.ReadLine());
  62. 15         }
  63. 16         bool isPrime = true;
  64. 17         int counter = 1;
  65. 18         //Trial division
  66. 19         while (counter <= Math.Sqrt(numberN))
  67. 20         {
  68. 21             if (numberN % counter == 0 && counter > 1)
  69. 22             {
  70. 23                 isPrime = false;
  71. 24             }
  72. 25             counter++;
  73. 26         }
  74. 27         Console.WriteLine("Is your number prime?\n{0}", isPrime);
  75. 28     }
  76. 29 }
  77.  
  78.  
  79. 10 10.InsideCircleOutsideRectangle
  80.  
  81. using System;
  82. 2
  83.  
  84. 3 class InsideCircleOutsideRectangle
  85. 4 {
  86. 5     //Write an expression that checks for given point (x, y) if it is within the circle K({1, 1}, 1.5) and out of the rectangle  
  87. 6     //R(top=1, left=-1, width=6, height=2).  
  88. 7     static void Main(string[] args)
  89. 8     {
  90. 9         Console.WriteLine("Enter coordinate x of the point");
  91. 10         decimal pointX = decimal.Parse(Console.ReadLine());
  92. 11         Console.WriteLine("Enter coordinate y of the point");
  93. 12         decimal pointY = decimal.Parse(Console.ReadLine());
  94. 13         decimal radius = 1.5m;
  95. 14         //Pythagorean Theorem a^2 + b^2 = c^2 ==> (x*x) + (y*y) <= radius * radius.
  96. 15         //Operator * is faster than Math.Pow()
  97. 16         bool isInCircle = (pointX - 1) * (pointX - 1) + (pointY - 1) * (pointY - 1) <= radius * radius;
  98. 17         //Every y below 1 is in the rectangle or outside the circle, or both.
  99. 18         if (isInCircle && pointY > 1)
  100. 19         {
  101. 20             Console.WriteLine("YES");
  102. 21         }
  103. 22         else
  104. 23         {
  105. 24             Console.WriteLine("NO");
  106. 25         }
  107. 26     }
  108. 27 }
  109.  
  110.  
  111. C# Homework: Console Input / Output 1-11/////////////////////////////////////////////////////////////////////////////////////////////////////
  112.  
  113.  
  114. 8 Problem 8.    Numbers from 1 to n
  115.  
  116.  
  117. 3.class NumberFromOneToN
  118. 4.{
  119. 5.    static void Main()
  120. 6.    {
  121. 7.        Console.Write("n=");
  122. 8.        int n = int.Parse(Console.ReadLine());
  123. 9.        for (int i = 1; i <= n; i++)
  124. 10.        {
  125. 11.            Console.WriteLine(i);
  126. 12.        }
  127. 13.    }
  128. 14.}
  129.  
  130. 9 Problem 9.    Sum of n Numbers
  131.  
  132. class SumOfNumbers
  133.  
  134. 5.    static void Main()
  135. 6.    {
  136. 7.        Console.Write("n=");
  137. 8.        int n = int.Parse(Console.ReadLine());
  138. 9.        double sum = 0;
  139. 10.        for (int i = 1; i <= n; i++)
  140. 11.        {
  141. 12.            Console.Write("number{0}=",i);
  142. 13.            double number = double.Parse(Console.ReadLine());
  143. 14.            sum += number;
  144. 15.        }
  145. 16.        Console.WriteLine("Sum={0}",sum);
  146. 17.    }
  147. 18.}
  148.  
  149. 10 fibonachi numbers
  150.  
  151.  
  152. 6.    {
  153. 7.        Console.Write("n=");
  154. 8.        int n = int.Parse(Console.ReadLine());
  155. 9.        int fiboTemA = 0;
  156. 10.        int fiboTempB = 1;
  157. 11.        for (int i = 0; i < n; i++)
  158. 12.        {
  159. 13.            Console.Write(fiboTemA + " ");      
  160. 14.            int temp = fiboTemA;
  161. 15.            fiboTemA = fiboTempB;
  162. 16.            fiboTempB = temp + fiboTempB;                  
  163. 17.        }
  164. 18.    }
  165.  
  166.  
  167. 11 Numbers in Interval Dividable by Given Number
  168.  
  169. 5.    static void Main()
  170. 6.    {
  171. 7.        Console.Write("Start= ");
  172. 8.        int a = int.Parse(Console.ReadLine());
  173. 9.        Console.Write("Stop= ");
  174. 10.        int b = int.Parse(Console.ReadLine());
  175. 11.        for (int i = a; i <= b; i++)
  176. 12.        {
  177. 13.            if (i % 5 == 0)
  178. 14.            {
  179. 15.                Console.Write(i);
  180. 16.                if (i < b - 2)
  181. 17.                {
  182. 18.                    Console.Write(",");
  183. 19.                }
  184. 20.           }
  185. 21.
  186. 22.        }
  187.  
  188. Conditional Statements//////////////////////////////////////////////////////////////////////////////////////////////////
  189.  
  190.  
  191. 2 BonusScore
  192. 4.{
  193. 5.    static void Main ()
  194. 6.    {
  195. 7.        Console.Write("Please, enter a whole number in the range [1 ... 9], SCORE = ");
  196. 8.        string inputStr = Console.ReadLine();
  197. 9.        int score = int.Parse(inputStr);
  198. 10.
  199. 11.        if (score > 0 && score < 10)
  200. 12.        {
  201. 13.            if (score >= 1 && score <= 3)
  202. 14.            {
  203. 15.                Console.WriteLine("Your Final Score is: {0} !", (score * 10));
  204. 16.            }
  205. 17.            else if (score >= 4 && score <= 6)
  206. 18.            {
  207. 19.                Console.WriteLine("Your Final Score is: {0} !", (score * 100));
  208. 20.            }
  209. 21            else
  210. 22.            {
  211. 23.                Console.WriteLine("Your Final Score is: {0} !", (score * 1000));
  212. 24.            }
  213. 25.        }
  214. 26.        else
  215. 27.        {
  216. 28.            Console.WriteLine("Input Error - Invalid score !!!");
  217. 29.        }
  218. 30.        Console.ReadLine();
  219. 31.    }
  220.  
  221.  
  222. 3
  223.  
  224. class CheckForAPlayCard
  225. 4.{
  226. 5.    static void Main ()
  227. 6.    {
  228. 7.        Console.Write("Please, enter a character for a sign your play card, CARD: ");
  229. 8.        string card = Console.ReadLine();
  230. 9.        string answer;
  231. 10.        switch (card)
  232. 11.        {
  233. 12.            case "2":
  234. 13.            case "3":
  235. 14.            case "4":
  236. 15.            case "5":
  237. 16.            case "6":
  238. 17.            case "7":
  239. 18.            case "8":
  240. 19.            case "9":
  241. 20.            case "10":
  242. 21.            case "J":
  243. 22.            case "Q":
  244. 23.            case "K":
  245. 24.            case "A":
  246. 25.                answer = "Yes";
  247. 26.                break;
  248. 27.            default:
  249. 28.                answer = "No";
  250. 29.                break;
  251. 30.        }
  252. 31.        Console.WriteLine("That card is a valid Play Card: \"{0}\" !", answer);
  253. 32.        Console.ReadLine();
  254. 33.    }
  255.  
  256.  
  257. 4
  258. 4  class MultiplicationSign
  259. 4.{
  260. 5.    static void Main ()
  261. 6.    {
  262. 7.        Console.Write("Please, enter the First real number, A = ");
  263. 8.        string numberStr = Console.ReadLine();
  264. 9.        decimal numA = decimal.Parse(numberStr);
  265. 10.        Console.Write("Enter the Second real number, B = ");
  266. 11.        numberStr = Console.ReadLine();
  267. 12.        decimal numB = decimal.Parse(numberStr);
  268. 13.        Console.Write("Enter the Third real number, C = ");
  269. 14.        numberStr = Console.ReadLine();
  270. 15.        decimal numC = decimal.Parse(numberStr);
  271. 16.
  272. 17.        char signNum;
  273. 18.        if (numA != 0 && numB != 0 && numC != 0)
  274. 19.        {
  275. 20.            if ((numA > 0 && numB > 0 && numC > 0) || (numA > 0 && numB < 0 && numC < 0)
  276. 21.                || (numA < 0 && numB > 0 && numC < 0) || (numA < 0 && numB < 0 && numC > 0))
  277. 22.            {
  278. 23.                signNum = '+';
  279. 24.            }
  280. 25.            else
  281. 26.            {
  282. 27.                signNum = '-';
  283. 28.            }
  284. 29.        }
  285. 30.        else
  286. 31.        {
  287. 32.            signNum = '0';
  288. 33.        }
  289. 34.        Console.WriteLine("The Sign of Multiplication of these 3 numbers is: SIGN = {0} !",
  290. 35.            signNum);
  291. 36.        Console.ReadLine();
  292. 37.    }
  293.  
  294.  
  295. 5
  296.  
  297. 5 class TheBiggestOf3Numbers
  298. 4.{
  299. 5.    static void Main ()
  300. 6.    {
  301. 7.        Console.Write("Please, enter the First number, A = ");
  302. 8.        string numberStr = Console.ReadLine();
  303. 9.        decimal numA = decimal.Parse(numberStr);
  304. 10.        Console.Write("Enter the Second number, B = ");
  305. 11.        numberStr = Console.ReadLine();
  306. 12.        decimal numB = decimal.Parse(numberStr);
  307. 13.        Console.Write("Enter the Third number, C = ");
  308. 14.        numberStr = Console.ReadLine();
  309. 15.        decimal numC = decimal.Parse(numberStr);
  310. 16.
  311. 17.        decimal theBiggest;
  312. 18.        if (numA >= numB)
  313. 19.        {
  314. 20.            if (numA >= numC)
  315. 21.            {
  316. 22.                theBiggest = numA;
  317. 23.            }
  318. 24.            else
  319. 25.            {
  320. 26.                theBiggest = numC;
  321. 27.            }        
  322. 28.        }
  323. 29.        else
  324. 30.        {
  325. 31.            if (numB >= numC)
  326. 32.            {
  327. 33.                theBiggest = numB;
  328. 34.            }
  329. 35.            else
  330. 36.            {
  331. 37.                theBiggest = numC;
  332. 38.            }
  333. 39.        }
  334. 40.        Console.WriteLine("The Biggest from these 3 numbers is THE BIGGEST = {0} !",
  335. 41.            theBiggest);
  336. 42.        Console.ReadLine();
  337. 43.    }
  338.  
  339.  
  340. 6
  341.  
  342.  
  343. 6  class TheBiggestOfFiveNumbers
  344. 4.{
  345. 5.    static void Main ()
  346. 6    {
  347. 7.        Console.Write("Please, enter the First number, A = ");
  348. 8.        string numberStr = Console.ReadLine();
  349. 9.        decimal numA = decimal.Parse(numberStr);
  350. 10.        Console.Write("Enter the Second number, B = ");
  351. 11.        numberStr = Console.ReadLine();
  352. 12.        decimal numB = decimal.Parse(numberStr);
  353. 13.        Console.Write("Enter the next number, C = ");
  354. 14.        numberStr = Console.ReadLine();
  355. 15.        decimal numC = decimal.Parse(numberStr);
  356. 16.        Console.Write("Enter the next number, D = ");
  357. 17.        numberStr = Console.ReadLine();
  358. 18.        decimal numD = decimal.Parse(numberStr);
  359. 19.        Console.Write("Enter the next number, E = ");
  360. 20.        numberStr = Console.ReadLine();
  361. 21.        decimal numE = decimal.Parse(numberStr);
  362. 22.
  363. 23.        decimal theBiggest;
  364. 24.        if (numA >= numB)
  365. 25.        {
  366. 26.            theBiggest = numA;
  367. 27.        }
  368. 28.        else
  369. 29.        {
  370. 30.            theBiggest = numB;
  371. 31.        }
  372. 32.        if (numC >= theBiggest)
  373. 33.        {
  374. 34.            theBiggest = numC;
  375. 35.        }
  376. 36.        if (numD >= theBiggest)
  377. 37.        {
  378. 38.            theBiggest = numD;
  379. 39.        }
  380. 40.        if (numE >= theBiggest)
  381. 41.        {
  382. 42.            theBiggest = numE;
  383. 43.        }
  384. 44.        Console.WriteLine("The Biggest from these 5 numbers is THE BIGGEST = {0} !",
  385. 45.            theBiggest);
  386. 46.        Console.ReadLine();
  387. 47.    }
  388. 48.}
  389.  
  390.  
  391. 7
  392.  
  393.  
  394. 7 class Sort3NumbersWithNestedIfs
  395.  
  396. 4.{
  397. 5.    static void Main ()
  398. 6.    {
  399. 7.        Console.Write("Please, enter the First real number, A = ");
  400. 8.        string numberStr = Console.ReadLine();
  401. 9.        decimal numA = decimal.Parse(numberStr);
  402. 10.        Console.Write("Enter the Second real number, B = ");
  403. 11.        numberStr = Console.ReadLine();
  404. 12.        decimal numB = decimal.Parse(numberStr);
  405. 13.        Console.Write("Enter the Third real number, C = ");
  406. 14.        numberStr = Console.ReadLine();
  407. 15.        decimal numC = decimal.Parse(numberStr);
  408. 16.
  409. 17.        decimal theBiggest = 0;
  410. 18.        decimal theMiddle = 0;
  411. 19.        decimal theSmallest = 0;
  412. 20.        if (numA >= numC && numB >= numC)
  413. 21.        {
  414. 22.            theSmallest = numC;
  415. 23.            if (numA >= numB)
  416. 24.            {
  417. 25.                theBiggest = numA;
  418. 26.                theMiddle = numB;
  419. 27.            }
  420. 28.            else
  421. 29.            {
  422. 30.                theBiggest = numB;
  423. 31.                theMiddle = numA;
  424. 32.            }
  425. 33.        }
  426. 34.        else if (numA >= numB && numB <= numC)
  427. 35.        {
  428. 36.            theSmallest = numB;
  429. 37.            if (numA >= numC)
  430. 38.            {
  431. 39.                theBiggest = numA;
  432. 40.                theMiddle = numC;
  433. 41.            }
  434. 42.            else
  435. 43.            {
  436. 44.                theBiggest = numC;
  437. 45.                theMiddle = numA;
  438. 46.            }
  439. 47.        }
  440. 48.        else
  441. 49.        {
  442. 50.            theSmallest = numA;
  443. 51.            if (numB >= numC)
  444. 52.            {
  445. 53.                theBiggest = numB;
  446. 54.                theMiddle = numC;
  447. 55.            }
  448. 56.            else
  449. 57.            {
  450. 58.                theBiggest = numC;
  451. 59.                theMiddle = numB;
  452. 60.            }
  453. 61.        }
  454. 62.        Console.WriteLine("These 3 numbers, sorted in Descending order are: {0}, {1}, {2} !",
  455. 63.            theBiggest, theMiddle, theSmallest);
  456. 64.        Console.ReadLine();
  457. 65.    }
  458. 66.}
  459.  
  460.  
  461. 8
  462.  
  463.  
  464.  8 nclass DigitAsWord
  465. 4.{
  466. 5.    static void Main ()
  467. 6.    {
  468. 7.        Console.Write("Please, enter a one digit number (0-9), DIGIT = ");
  469. 8.        string digit = Console.ReadLine();
  470. 9.        string result;
  471. 10.        switch (digit)
  472. 11.        {
  473. 12.            case "0":
  474. 13.                result = "ZERO";
  475. 14.                break;
  476. 15.            case "1":
  477. 16.                result = "ONE";
  478. 17.                break;
  479. 18.            case "2":
  480. 19.                result = "TWO";
  481. 20.                break;
  482. 21.            case "3":
  483. 22.                result = "THREE";
  484. 23.                break;
  485. 24.            case "4":
  486. 25.                result = "FOUR";
  487. 26.                break;
  488. 27.            case "5":
  489. 28.                result = "FIVE";
  490. 29.                break;
  491. 30.            case "6":
  492. 31.                result = "SIX";
  493. 32.                break;
  494. 33.            case "7":
  495. 34.                result = "SEVEN";
  496. 35.                break;
  497. 36.            case "8":
  498. 37.                result = "EIGHT";
  499. 38.                break;
  500. 39.            case "9":
  501. 40.                result = "NINE";
  502. 41.                break;
  503. 42.            default:
  504. 43.                result = "Not a digit !";
  505. 44.                break;
  506. 45.        }
  507. 46.        Console.WriteLine("That digit is - {0} ", result);
  508. 47.        Console.ReadLine();
  509. 48.    }
  510.  
  511.  
  512. 10
  513.  
  514.  
  515. 10  BeerTime
  516.  
  517. 2.using System.Threading;
  518. 3.using System.Globalization;
  519. 4.
  520. 5.class BeerTime
  521. 6.{
  522. 7.    static void Main ()
  523. 8.    {
  524. 9.        Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  525. 10.        Console.Write(@"Please, enter your time in format ""hh:mm TT"",
  526. 11.(example 05:30 PM),  YOUR TIME = ");
  527. 12.        string timeStr = Console.ReadLine();
  528. 13.        DateTime customTime;
  529. 14.        if (DateTime.TryParse(timeStr, out customTime))
  530. 15.            Console.WriteLine("Converted '{0}' to {1} ({2}).", timeStr, customTime,
  531. 16.                              customTime.Kind);
  532. 17.        else
  533. 18.        {
  534. 19.            Console.WriteLine("'{0}' is not in an acceptable format.", timeStr);
  535. 20.            Console.WriteLine("Error - Invalid Time !!!");
  536. 21.            Console.ReadLine();
  537. 22.            return;
  538. 23.        }
  539. 24.
  540. 25.        DateTime startBeerTime = DateTime.Parse("01:00 PM");
  541. 26.        DateTime endBeerTime = DateTime.Parse("03:00 AM");
  542. 27.        if ((customTime >= endBeerTime) && (customTime < startBeerTime))
  543. 28.        {
  544. 29.            Console.WriteLine("Now is NON-BEER TIME !");
  545. 30.        }
  546. 31.        else
  547. 32.        {
  548. 33.            Console.WriteLine("Now is BEER TIME !");
  549. 34.        }      
  550. 35.        Console.ReadLine();
  551. 36.    }
  552. 37.}
  553.  
  554.  
  555. FOR LOOP //////////////////////////////////////////////////////////////////////////
  556.  
  557.  
  558. 2 class NumbersNotDivisibleBy3And7/////
  559.  
  560.  
  561.  
  562. 8.        int number = int.Parse(Console.ReadLine());
  563. 9.
  564. 10.        for (int i = 1; i <= number; i++)
  565. 11.        {
  566. 12.            if (i % 3 == 0 || i % 7 == 0)
  567. 13.            {
  568. 14.                continue;
  569. 15.            }
  570. 16.            else
  571. 17.            {
  572. 18.                Console.Write(i + " ");
  573. 19.            }
  574. 20.        }
  575. 21.
  576. 22.        Console.WriteLine();
  577.  
  578.  
  579. Problem 3.  Min, Max, Sum and Average of N Numbers
  580.  
  581.  
  582. 8.  int length = int.Parse(Console.ReadLine());
  583. 9.
  584. 10.        int min = int.MaxValue;
  585. 11.        int max = int.MinValue;
  586. 12.        double sum = 0;
  587. 13.        double avg = 0;
  588. 14.
  589. 15.        for (int i = 0; i < length; i++)
  590. 16.        {
  591. 17.            int number = int.Parse(Console.ReadLine());
  592. 18.
  593. 19.            //min
  594. 20.            min = Math.Min(min, number);
  595. 21.
  596. 22.            //max
  597. 23.            max = Math.Max(max, number);
  598. 24.
  599. 25.            //sum
  600. 26.            sum += number;
  601. 27.
  602. 28.            //avg
  603. 29.            avg = sum / length;
  604. 30.        }
  605. 31.
  606. 32.        Console.WriteLine("Min = " + min);
  607. 33.        Console.WriteLine("Max = " + max);
  608. 34.        Console.WriteLine("Sum = " + sum);
  609. 35.        Console.WriteLine("Avg = {0:F2}", avg);
  610. 36.    }
  611.  
  612.  
  613. Problem 4.  Print a Deck of 52 Cards
  614.  
  615.  
  616. string[] cards = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
  617. 8.        int[] colors = { 3, 4, 5, 6 };
  618. 9.
  619. 10.        foreach (var card in cards)
  620. 11.        {
  621. 12.            foreach (var color in colors)
  622. 13.            {
  623. 14.                Console.Write(card);
  624. 15.                Console.Write((char)color + " ");
  625. 16.            }
  626. 17.
  627. 18.            Console.WriteLine();
  628.  
  629.  
  630.  
  631. Problem 5.  Calculate 1 + 1!/X + 2!/X2 ++ N!/XN
  632.  
  633. 7.          Console.Write("n = ");
  634. 8.        int n = int.Parse(Console.ReadLine());
  635. 9.        Console.Write("x = ");
  636. 10.        int x = int.Parse(Console.ReadLine());
  637. 11.
  638. 12.        double factN = 1;
  639. 13.        double multiX = 1;
  640. 14.        double result = 1;
  641. 15.
  642. 16.        for (int i = 1; i <= n; i++)
  643. 17.        {
  644. 18.            factN *= i;
  645. 19.            multiX *= x;
  646. 20.            result += factN / multiX;
  647. 21.        }
  648. 22.
  649. 23.        Console.WriteLine("{0:F5}", result);
  650. 24.    }
  651.  
  652.  
  653. Problem 6.  Calculate N! / K!
  654.  
  655. 8.        Console.Write("n = ");
  656. 9.        int n = int.Parse(Console.ReadLine());
  657. 10.        Console.Write("k = ");
  658. 11.        int k = int.Parse(Console.ReadLine());
  659. 12.
  660. 13.        if (n > 1 && k > 1 && n < 100 && k < 100)
  661. 14.        {
  662. 15.            BigInteger factN = 1;
  663. 16.            BigInteger factK = 1;
  664. 17.            BigInteger result = 0;
  665. 18.
  666. 19.            for (int i = 1; i <= n; i++)
  667. 20.            {
  668. 21.                factN *= i;
  669. 22.            }
  670. 23.
  671. 24.            for (int j = 1; j <= k; j++)
  672. 25.            {
  673. 26.                factK *= j;
  674. 27.            }
  675. 28.
  676. 29.            result = factN / factK;
  677. 30.
  678. 31.            Console.WriteLine(result);
  679. 32.        }
  680. 33.        else
  681. 34.        {
  682. 35.            Console.WriteLine("Invalid input!");
  683. 36.        }
  684.  
  685.  
  686.  
  687.         7 Problem 7.    Calculate N! / (K! * (N-K)!)
  688.  
  689.  
  690. 5.{
  691. 6.    static void Main()
  692. 7.    {
  693. 8.        Console.Write("n = ");
  694. 9.        int n = int.Parse(Console.ReadLine());
  695. 10.        Console.Write("k = ");
  696. 11.        int k = int.Parse(Console.ReadLine());
  697. 12.
  698. 13.        if (n > 1 && k > 1 && n < 100 && k < 100)
  699. 14.        {
  700. 15.            BigInteger factN = 1;
  701. 16.            BigInteger factK = 1;
  702. 17.            BigInteger factNAndK = 1;
  703. 18.            BigInteger result = 0;
  704. 19.
  705. 20.            for (int i = 1; i <= n; i++)
  706. 21.            {
  707. 22.                factN *= i;
  708. 23.            }
  709. 24.
  710. 25.            for (int j = 1; j <= k; j++)
  711. 26.            {
  712. 27.                factK *= j;
  713. 28.            }
  714. 29.
  715. 30.            for (int z = 1; z <= n - k; z++)
  716. 31.            {
  717. 32.                factNAndK *= z;
  718. 33.            }
  719. 34.
  720. 35.            result = factN / (factK * factNAndK);
  721. 36.
  722. 37.            Console.WriteLine(result);
  723. 38.        }
  724. 39.        else
  725. 40.        {
  726. 41.            Console.WriteLine("Invalid input!");
  727. 42.        }
  728.  
  729.  
  730.  
  731. Problem 8.  Catalan Numbers
  732.  
  733. 8.        Console.Write("n = ");
  734. 9.        int n = int.Parse(Console.ReadLine());
  735. 10.
  736. 11.        if (n > 1 && n < 100)
  737. 12.        {
  738. 13.            BigInteger multiFactN = 1;
  739. 14.            BigInteger plusFactN = 1;
  740. 15.            BigInteger factN = 1;
  741. 16.            BigInteger result = 0;
  742. 17.
  743. 18.            for (int i = 1; i <= 2 * n; i++)
  744. 19.            {
  745. 20.                multiFactN *= i;
  746. 21.            }
  747. 22.
  748. 23.            for (int j = 1; j <= n + 1; j++)
  749. 24.            {
  750. 25.                plusFactN *= j;
  751. 26.            }
  752. 27.
  753. 28.            for (int k = 1; k <= n; k++)
  754. 29.            {
  755. 30.                factN *= k;
  756. 31.            }
  757. 32.
  758. 33.            result = multiFactN / (plusFactN * factN);
  759. 34.
  760. 35.            Console.WriteLine(result);
  761. 36.        }
  762. 37.        else
  763. 38.        {
  764. 39.            Console.WriteLine("Invalid input!");
  765. 40.        }
  766.  
  767.  
  768.  
  769. Problem 9.  Matrix of Numbers
  770.  
  771. 7.        Console.Write("n = ");
  772. 8.        int n = int.Parse(Console.ReadLine());
  773. 9.
  774. 10.        if (n > 1 && n < 20)
  775. 11.        {
  776. 12.            for (int i = 0; i < n; i++)
  777. 13.            {
  778. 14.                for (int j = i + 1; j <= n + i; j++)
  779. 15.                {
  780. 16.                    Console.Write(j + " ");
  781. 17.                }
  782. 18.
  783. 19.                Console.WriteLine();
  784. 20.            }
  785. 21.        }
  786. 22.        else
  787. 23.        {
  788. 24.            Console.WriteLine("Invalid input!");
  789. 25.        }
  790.  
  791.  
  792.  
  793.  
  794. Problem 10. Odd and Even Product
  795.  
  796. 7.          int even = 1;
  797. 8.        int odd = 1;
  798. 9.
  799. 10.        string readNumbers = Console.ReadLine();
  800. 11.        string[] numbers = readNumbers.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  801. 12.
  802. 13.        for (int i = 0; i < numbers.Length; i++)
  803. 14.        {
  804. 15.            int number = int.Parse(numbers[i]);
  805. 16.
  806. 17.            if (i % 2 == 0)
  807. 18.            {
  808. 19.                even *= number;
  809. 20.            }
  810. 21.            else
  811. 22.            {
  812. 23.                odd *= number;
  813. 24.            }
  814. 25.        }
  815. 26.
  816. 27.        if (even == odd)
  817. 28.        {
  818. 29.            Console.WriteLine("yes");
  819. 30.            Console.WriteLine("product = " + even);
  820. 31.        }
  821. 32.        else
  822. 33.        {
  823. 34.            Console.WriteLine("no");
  824. 35.            Console.WriteLine("odd_product = " + odd);
  825. 36.            Console.WriteLine("even_product = " + even);
  826. 37.        }
  827.  
  828.  
  829.  
  830. Problem 11. Random Numbers in Given Range
  831.  
  832. 7.        Console.Write("n = ");
  833. 8.        int n = int.Parse(Console.ReadLine());
  834. 9.        Console.Write("min = ");
  835. 10.        int min = int.Parse(Console.ReadLine());
  836. 11.        Console.Write("max = ");
  837. 12.        int max = int.Parse(Console.ReadLine());
  838. 13.
  839. 14.        if (min <= max)
  840. 15.        {
  841. 16.            Random random = new Random();
  842. 17.
  843. 18.            for (int i = 0; i < n; i++)
  844. 19.            {
  845. 20.                Console.Write(random.Next(min, max + 1) + " ");
  846. 21.            }
  847. 22.
  848. 23.            Console.WriteLine();
  849. 24.        }
  850.  
  851.  
  852. Problem 12. * Randomize the Numbers 1…N
  853.  
  854. 7.        Console.Write("n = ");
  855. 8.        int n = int.Parse(Console.ReadLine());
  856. 9.
  857. 10.        Random randomNumbers = new Random();
  858. 11.
  859. 12.        for (int i = 1; i <= n; i++)
  860. 13.        {
  861. 14.            Console.Write(randomNumbers.Next(1, n + 1) + " ");
  862. 15.        }
  863. 16.
  864. 17.        Console.WriteLine();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement