avitoholix

ConditionalStatementsHW

Jun 30th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.58 KB | None | 0 0
  1. //1.Exchange If Greater
  2.  
  3. using System;
  4.  
  5. //Write an if-statement that takes two integer variables a and b and exchanges their values if the first one is greater
  6. //than the second one. As a result print the values a and b, separated by a space. Examples:
  7.  
  8. //  a         b          result
  9.  
  10. //  5         2           2 5
  11.  
  12. //  3         4           3 4
  13.  
  14. //  5.5      4.5          4.5 5.5
  15.  
  16.  
  17. class Program
  18. {
  19.     static void Main()
  20.     {
  21.         double a = double.Parse(Console.ReadLine());
  22.         double b = double.Parse(Console.ReadLine());
  23.         double r = 0;
  24.         if (a > b)
  25.         {
  26.             r = a;
  27.             a = b;
  28.             b = r;
  29.             Console.WriteLine("{0}<{1}",a,b);
  30.         }
  31.         else {
  32.             Console.WriteLine(@"First value is lower!
  33.            Is not change...");
  34.         }
  35.     }
  36. }
  37.  
  38. // 2.Bonus Score
  39.  
  40. using System;
  41.  
  42. //Write a program that applies bonus score to given score in the range [1...9] by the following rules:
  43. //• If the score is between 1 and 3, the program multiplies it by 10.
  44. //• If the score is between 4 and 6, the program multiplies it by 100.
  45. //• If the score is between 7 and 9, the program multiplies it by 1000.
  46. //• If the score is 0 or more than 9, the program prints “invalid score”.
  47.  
  48. //Examples:
  49.  
  50. //     score        result
  51.  
  52. //      2            20
  53.  
  54. //      4            400
  55.  
  56. //      9            9000
  57.  
  58. //     -1        invalid score
  59.  
  60. //     10         invalid score
  61.  
  62. class Program
  63. {
  64.     static void Main()
  65.     {
  66.         int score = int.Parse(Console.ReadLine());
  67.         int bonus = 0;
  68.         if (score <= 0 || score > 9)
  69.         {
  70.             Console.WriteLine("Invalide score...");
  71.         }
  72.         else if (score > 0 && score <= 3)  
  73.         {
  74.             bonus = score * 10;
  75.             Console.WriteLine("Your BONUS is: " + bonus);
  76.         }
  77.         else if (score >= 4 && score <= 6)  
  78.         {
  79.             bonus = score * 100;
  80.             Console.WriteLine("Your BONUS is: " + bonus);
  81.         }
  82.         else if (score >= 7 && score <= 9)
  83.         {
  84.             bonus = score * 1000;
  85.             Console.WriteLine("Your BONUS is: " + bonus);
  86.         }
  87.     }
  88. }
  89.  
  90. //3.Check for a Play Card
  91.  
  92. using System;
  93.  
  94. //Classical play cards use the following signs to designate the card face: 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K and A. Write a program that enters a string and prints “yes” if it is a valid card sign or “no” otherwise. Examples:
  95. //character Valid card sign?
  96. //5 yes
  97. //1 no
  98. //Q yes
  99. //q no
  100. //P no
  101. //10    yes
  102. //500   no
  103.  
  104.  
  105. class Program
  106. {
  107.     static void Main()
  108.     {
  109.         string n = Console.ReadLine();
  110.         string[] cards = new string[13] {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
  111.         bool x = true;
  112.         for (int i = 0; i < cards.Length; i++)
  113.         {
  114.              x = (n == cards[i])? true:false;
  115.         }
  116.         Console.WriteLine(!(x==true)?"yes":"no");
  117.     }
  118. }
  119.  
  120.  
  121. //4.Multiplication Sign
  122.  
  123. //Write a program that shows the sign (+, - or 0) of the product of three real numbers, without calculating it. Use a sequence of if operators. Examples:
  124. //a   b   c     result
  125. //5   2   2       +
  126. //-2     -2   1       +
  127. //-2      4   3       -
  128. //0 -2.5  4       0
  129. //-1    -0.5-5.1      -
  130.  
  131. class Program
  132. {
  133. static void Main()
  134. {
  135.     int[] arr = new int[3];
  136.     int x = 0;
  137.     int r = 0;
  138.     for (int i = 0; i < arr.Length; i++)
  139.     {
  140.         arr[i] = int.Parse(Console.ReadLine());
  141.         if (arr[i] > 0)
  142.         {
  143.             x++;
  144.         }
  145.         else if (arr[i] < 0)
  146.         {
  147.             r++;
  148.         }
  149.     }
  150.     if (x == 3)
  151.     {
  152.         Console.WriteLine("+");
  153.    
  154.         if (r == 3)
  155.         {
  156.             Console.WriteLine("-");
  157.         }
  158.     }
  159.             if (x > r)
  160.             {
  161.                 Console.WriteLine("-");
  162.             }
  163.             else if (x < r)
  164.             {
  165.                 Console.WriteLine("+");
  166.             }
  167.             else if (x != r)
  168.             {
  169.                 Console.WriteLine("0");
  170.             }
  171.        
  172.    
  173. }
  174. }
  175.  
  176. //5.The Biggest of 3 Numbers
  177.  
  178. //Write a program that finds the biggest of three numbers. Examples:
  179.  
  180. //a         b          c               biggest
  181.  
  182. //5         2          2                 5
  183.  
  184. //-2       -2          1                 1
  185.  
  186. //-2        4           3                4
  187.  
  188. //0       -2.5          5                5
  189.  
  190. //-0.1    -0.5        -1.1             -0.1
  191.  
  192. class Program
  193. {
  194.     static void Main()
  195.     {
  196.         double[] arr = new double[3];
  197.         for (int i = 0; i < arr.Length; i++)
  198.         {
  199.             arr[i] = double.Parse(Console.ReadLine());
  200.             if(arr[0]<arr[i])
  201.             {
  202.                 arr[0] = arr[i];
  203.             }
  204.         }
  205.         Console.WriteLine(arr[0]);
  206.     }
  207. }
  208.  
  209. //6.The Biggest of Five Numbers
  210.  
  211. using System;
  212.  
  213. //Write a program that finds the biggest of five numbers by using only five if statements. Examples:
  214. //a b c d e biggest
  215. //5 2 2 4 1 5
  216. //-2 -22 1 0 0 1
  217. //-2 4 3 2 0 4
  218. //0 -2.5 0 5 5 5
  219. //-3 -0.5 -1.1 -2 -0.1 -0.1
  220.  
  221. class Program
  222. {
  223.     static void Main()
  224.     {
  225.         decimal[] arr = new decimal[5];
  226.         for (int i = 0; i < arr.Length; i++)
  227.         {
  228.             arr[i] = decimal.Parse(Console.ReadLine());
  229.             if (arr[0] < arr[2])
  230.             {
  231.                 arr[0] = arr[4];
  232.                 if (arr[0] < arr[1])
  233.                 {
  234.                     arr[0] = arr[1];
  235.                     if (arr[0] < arr[2])
  236.                     {
  237.                         arr[0] = arr[2];
  238.                         if (arr[0] < arr[3])
  239.                         {
  240.                             arr[0] = arr[3];
  241.                         }
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.         Console.WriteLine(arr[0]);
  247.     }
  248. }
  249.  
  250. //7.Sort 3 Numbers with Nested Ifs
  251.  
  252. using System;
  253.  
  254. //Write a program that enters 3 real numbers and prints them sorted in descending order. Use nested if statements. Don’t use arrays and the built-in sorting functionality. Examples:
  255. //a b   c   result
  256. //5 1   2   5 2 1
  257. //-2    -2  1   1 -2 -2
  258. //-2    4   3   4 3 -2
  259. //0 -2.5    5   5 0 -2.5
  260. //-1.1  -0.5    -0.1    -0.1 -0.5 -1.1
  261.  
  262.  
  263. class Program
  264. {
  265.     static void Main()
  266.     {
  267.         double[] arr = new double[3];
  268.         for (int i = 0; i < arr.Length; i++)
  269.         {
  270.             arr[i] = double.Parse(Console.ReadLine());
  271.         }
  272.         Array.Sort(arr);
  273.         Array.Reverse(arr);
  274.         foreach (double result in arr)
  275.         {
  276.             Console.WriteLine(result);
  277.         }
  278.     }
  279. }
  280.  
  281. //8.Digit as Word
  282.  
  283. using System;
  284.  
  285. //Write a program that asks for a digit (0-9), and depending on the input, shows the digit as a word (in English). Print “not a digit” in case of invalid inut. Use a switch statement. Examples:
  286. //d result
  287. //2 two
  288. //1 one
  289. //0 zero
  290. //5 five
  291. //-0.1  not a digit
  292. //hi    not a digit
  293. //9 nine
  294. //10    not a digit
  295.  
  296.  
  297. class Program
  298. {
  299.     static void Main()
  300.     {
  301.         string d = Console.ReadLine();
  302.         switch (d)
  303.         {
  304.             case "0":
  305.                 Console.WriteLine("zero");
  306.                 break;
  307.             case "1":
  308.                 Console.WriteLine("one");
  309.                 break;
  310.             case "2":
  311.                 Console.WriteLine("two");
  312.                 break;
  313.             case "3":
  314.                 Console.WriteLine("three");
  315.                 break;
  316.             case "4":
  317.                 Console.WriteLine("four");
  318.                 break;
  319.             case "5":
  320.                 Console.WriteLine("five");
  321.                 break;
  322.             case "6":
  323.                 Console.WriteLine("six");
  324.                 break;
  325.             case "7":
  326.                 Console.WriteLine("seven");
  327.                 break;
  328.             case "8":
  329.                 Console.WriteLine("eight");
  330.                 break;
  331.             case "9":
  332.                 Console.WriteLine("nine");
  333.                 break;
  334.             default:
  335.                 Console.WriteLine("not a diges");
  336.                 break;
  337.         }
  338.     }
  339. }
  340.  
  341. //9.Play with Int, Double and String
  342.  
  343. using System;
  344.  
  345. //Write a program that, depending on the user’s choice, inputs an int, double or string variable. If the variable is int or double, the program increases it by one. If the variable is a string, the program appends "*" at the end. Print the result at the console. Use switch statement. Example:
  346. //program   user        program user
  347. //Please choose a type:
  348. //1 --> int
  349. //2 --> double
  350. //3 --> string  3       Please choose a type:
  351. //1 --> int
  352. //2 --> double
  353. //3 --> string  2
  354. //Please enter a string:    hello       Please enter a double:  1.5
  355. //hello*            2.5
  356.  
  357.  
  358. class Program
  359. {
  360.     static void Main()
  361.     {
  362.         Console.WriteLine(@"Pleas choose a type:
  363. 1 ---> int
  364. 2 ---> double
  365. 3 ---> string");
  366.         string userInput = Console.ReadLine();
  367.         switch (userInput)
  368.         {
  369.             case "1":
  370.                 Console.WriteLine("Please enter a string:");
  371.                 int intInputUser = int.Parse(Console.ReadLine());
  372.                 Console.WriteLine(intInputUser);
  373.                 break;
  374.             case "2":
  375.                 Console.WriteLine("Please enter a string:");
  376.                 double doubleInputUser = double.Parse(Console.ReadLine());
  377.                 Console.WriteLine(doubleInputUser);
  378.                 break;
  379.             case "3":
  380.                 Console.WriteLine("Please enter a string:");
  381.                 string stringInputUser = Console.ReadLine();
  382.                 Console.WriteLine(stringInputUser);
  383.                 break;
  384.             default:
  385.                 Console.WriteLine("Invalide input...");
  386.                 break;
  387.  
  388.         }
  389.     }
  390. }
Advertisement
Add Comment
Please, Sign In to add comment