Advertisement
EmoRz

C# CONDITIONAL STATEMENTS AND LOOPS - EXERCISES

May 16th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.53 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Exercises_Condition_Loop
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.  
  10.         }
  11.         private static void NeighbourWars()
  12.         {
  13.             var peshoDamage = int.Parse(Console.ReadLine());
  14.             var goshoDamage = int.Parse(Console.ReadLine());
  15.             //
  16.             var peshoHealth = 100;
  17.             var goshoHealth = 100;
  18.             //
  19.             var round = 0;
  20.             while (true)
  21.             {
  22.                 if (round % 2 == 0)
  23.                 {
  24.                     round++;
  25.                     goshoHealth -= peshoDamage;
  26.                     if (goshoHealth <= 0)
  27.                     {
  28.                         Console.WriteLine($"Pesho won in {round}th round.");
  29.                         break;
  30.                     }
  31.                     else
  32.                     {
  33.                         Console.Write("Pesho used Roundhouse kick and reduced ");
  34.                         Console.WriteLine($"Gosho to {goshoHealth} health.");
  35.                     }
  36.                 }
  37.                 else
  38.                 {
  39.                     round++;
  40.                     peshoHealth -= goshoDamage;
  41.                     if (peshoHealth <= 0)
  42.                     {
  43.                         Console.WriteLine($"Gosho won in {round}th round.");
  44.                         break;
  45.                     }
  46.                     else
  47.                     {
  48.                         Console.Write("Gosho used Thunderous fist and reduced");
  49.                         Console.WriteLine($" Pesho to {peshoHealth} health.");
  50.                     }
  51.                 }
  52.                 if (round % 3 == 0 && (peshoHealth > 0 || goshoHealth > 0))
  53.                 {
  54.                     peshoHealth += 10;
  55.                     goshoHealth += 10;
  56.                 }
  57.             }
  58.         }
  59.  
  60.  
  61.         private static void Macig_Number()
  62.         {
  63.             char a = char.Parse(Console.ReadLine());
  64.             char b = char.Parse(Console.ReadLine());
  65.             char stop = char.Parse(Console.ReadLine());
  66.             //
  67.             for (char i = a; i <= b; i++)
  68.             {
  69.                 for (char ii = a; ii <= b; ii++)
  70.                 {
  71.                     for (char iii = a; iii <= b; iii++)
  72.                     {
  73.                         if (i != stop && ii != stop && iii != stop)
  74.                         {
  75.                             Console.Write($"{i}{ii}{iii} ");
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.         }
  81.  
  82.         private static void GameOfNumbers()
  83.         {
  84.             var num1 = int.Parse(Console.ReadLine());
  85.             var num2 = int.Parse(Console.ReadLine());
  86.             var stop = int.Parse(Console.ReadLine());
  87.             var isFoundNum = true;
  88.             var combination = 0;
  89.             var a = 0;
  90.             var b = 0;
  91.             var sum = 0;
  92.             for (int i = num1; i <= num2; i++)
  93.             {
  94.                 for (int ii = num1; ii <= num2; ii++)
  95.                 {
  96.                     combination++;
  97.                     if (i + ii == stop)
  98.                     {
  99.                         a = i;
  100.                         b = ii;
  101.                         sum = stop;
  102.                         isFoundNum = false;
  103.                     }
  104.                 }
  105.             }
  106.             if (isFoundNum)
  107.             {
  108.                 Console.WriteLine($"{combination} combinations - neither equals {stop}");
  109.             }
  110.             else
  111.             {
  112.                 Console.WriteLine($"Number found! {a} + {b} = {sum}");
  113.             }
  114.         }
  115.  
  116.         private static void Test_Numbers()
  117.         {
  118.             var num1 = int.Parse(Console.ReadLine());
  119.             var num2 = int.Parse(Console.ReadLine());
  120.             var stop = int.Parse(Console.ReadLine());
  121.             //
  122.             var product = 0;
  123.             var temp = 0;
  124.             var combination = 0;
  125.             for (int i = num1; i >= 1; i--)
  126.             {
  127.                 for (int ii = 1; ii <= num2; ii++)
  128.                 {
  129.                     if (product > stop)
  130.                     {
  131.                         break;
  132.                     }
  133.                     else
  134.                     {
  135.                         temp = 3 * (i * ii);
  136.                         product += temp;
  137.                         combination++;
  138.                     }
  139.                 }
  140.             }
  141.             Console.WriteLine($"{combination} combinations");
  142.  
  143.             if (product < stop)
  144.             {
  145.                 Console.WriteLine($"Sum: {product}");
  146.             }
  147.             else
  148.             {
  149.                 Console.WriteLine($"Sum: {product} >= {stop}");
  150.             }
  151.         }
  152.  
  153.         private static void Five_Diff_Numbers()
  154.         {
  155.             var a = int.Parse(Console.ReadLine());
  156.             var b = int.Parse(Console.ReadLine());
  157.             var isThereInterval = true;
  158.             for (int i = a; i <= b; i++)
  159.             {
  160.                 for (int ii = a; ii < b; ii++)
  161.                 {
  162.                     for (int iii = a; iii < b; iii++)
  163.                     {
  164.                         for (int iiii = a; iiii < b; iiii++)
  165.                         {
  166.                             for (int iiiii = a; iiiii <= b; iiiii++)
  167.                             {
  168.                                 if (a <= i && i < ii && ii < iii && iii < iiii && iiii < iiiii && iiiii <= b)
  169.                                 {
  170.                                     Console.WriteLine($"{i} {ii} {iii} {iiii} {iiiii}");
  171.                                     isThereInterval = false;
  172.                                 }
  173.                             }
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.             if (isThereInterval)
  179.             {
  180.                 Console.WriteLine("No");
  181.             }
  182.         }
  183.  
  184.         private static void Num_Triangle()
  185.         {
  186.             var n = int.Parse(Console.ReadLine());
  187.  
  188.             for (int i = 1; i <= n; i++)
  189.             {
  190.                 for (int j = 1; j <= i; j++)
  191.                 {
  192.                     Console.Write(i + " ");
  193.                 }
  194.                 Console.WriteLine();
  195.             }
  196.         }
  197.  
  198.         private static void Count_Integers()
  199.         {
  200.             var count = 0;
  201.             while (true)
  202.             {
  203.                 var data = Console.ReadLine();
  204.                 try
  205.                 {
  206.                     if (int.Parse(data) % 2 == 0 || int.Parse(data) % 2 != 0)
  207.                     {
  208.                         count++;
  209.                     }
  210.                 }
  211.                 catch (Exception)
  212.                 {
  213.                     Console.WriteLine(count);
  214.                     break;
  215.                 }
  216.             }
  217.         }
  218.  
  219.         private static void Calories()
  220.         {
  221.             var n = int.Parse(Console.ReadLine());
  222.             var total = 0;
  223.             for (int i = 0; i < n; i++)
  224.             {
  225.                 string product = Console.ReadLine().ToLower();
  226.                 if (product == "cheese")
  227.                 {
  228.                     total += 500;
  229.                 }
  230.                 else if (product == "tomato sauce")
  231.                 {
  232.                     total += 150;
  233.                 }
  234.                 else if (product == "salami")
  235.                 {
  236.                     total += 600;
  237.                 }
  238.                 else if (product == "pepper")
  239.                 {
  240.                     total += 50;
  241.                 }
  242.             }
  243.             Console.WriteLine($"Total calories: {total}");
  244.         }
  245.  
  246.         private static void Cake()
  247.         {
  248.             string ingredient = Console.ReadLine();
  249.             var count = 0;
  250.             while (ingredient != "Bake!")
  251.             {
  252.                 count++;
  253.                 Console.WriteLine($"Adding ingredient {ingredient}.");
  254.                 ingredient = Console.ReadLine();
  255.             }
  256.             Console.WriteLine($"Preparing cake with {count} ingredients.");
  257.         }
  258.  
  259.         private static void Interval_Numbers()
  260.         {
  261.             var a = int.Parse(Console.ReadLine());
  262.             var b = int.Parse(Console.ReadLine());
  263.             //
  264.             var small = 0;
  265.             var big = 0;
  266.             //
  267.             if (a > b)
  268.             {
  269.                 big = a;
  270.                 small = b;
  271.             }
  272.             else
  273.             {
  274.                 big = b;
  275.                 small = a;
  276.             }
  277.             for (int i = small; i <= big; i++)
  278.             {
  279.                 Console.WriteLine(i);
  280.             }
  281.         }
  282.  
  283.         private static void Word_In_Plural()
  284.         {
  285.             var word = Console.ReadLine();
  286.             var newWord = "";
  287.             if (word.EndsWith("ch") || word.EndsWith("o") || word.EndsWith("s") || word.EndsWith("sh") || word.EndsWith("x") || word.EndsWith("z"))
  288.             {
  289.                 word += "es";
  290.             }
  291.             else if (word.EndsWith("y"))
  292.             {
  293.                 word = word.Remove(word.Length - 1);
  294.                 word += "ies";
  295.             }
  296.             else
  297.             {
  298.                 word += "s";
  299.             }
  300.             Console.WriteLine(word);
  301.         }
  302.  
  303.         private static void Hotel()
  304.         {
  305.             var month = Console.ReadLine();
  306.             var nightsCount = int.Parse(Console.ReadLine());
  307.             //
  308.             var studio = 0.0;
  309.             var doub = 0.0;
  310.             var suit = 0.0;
  311.             //
  312.             if (month == "May" || month == "October")
  313.             {
  314.                 studio = 50;
  315.                 doub = 65;
  316.                 suit = 75;
  317.             }
  318.             if (month == "June" || month == "September")
  319.             {
  320.                 studio = 60;
  321.                 doub = 72;
  322.                 suit = 82;
  323.             }
  324.             if (month == "July" || month == "December" || month == "August")
  325.             {
  326.                 studio = 68;
  327.                 doub = 77;
  328.                 suit = 89;
  329.             }
  330.             bool isFree = true;
  331.             if ((month == "May" || month == "October") && nightsCount > 7)
  332.             {
  333.                 studio *= 0.95;
  334.             }
  335.             if ((month == "June" || month == "September") && nightsCount > 14)
  336.             {
  337.                 doub *= 0.90;
  338.             }
  339.             if ((month == "July" || month == "December" || month == "August") && nightsCount > 14)
  340.             {
  341.                 suit *= 0.85;
  342.             }
  343.             if ((month == "September" || month == "October") && nightsCount > 7)
  344.             {
  345.                 nightsCount--;
  346.                 isFree = false;
  347.             }
  348.             if (isFree)
  349.             {
  350.                 studio *= nightsCount;
  351.                 doub *= nightsCount;
  352.                 suit *= nightsCount;
  353.             }
  354.             if (isFree == false)
  355.             {
  356.                 studio *= nightsCount;
  357.                 doub *= (nightsCount + 1);
  358.                 suit *= (nightsCount + 1);
  359.             }
  360.             //
  361.             Console.WriteLine($"Studio: {studio:f2} lv.");
  362.             Console.WriteLine($"Double: {doub:f2} lv.");
  363.             Console.WriteLine($"Suite: {suit:f2} lv.");
  364.         }
  365.  
  366.         private static void Reataurant_Discount()
  367.         {
  368.             var groupSize = int.Parse(Console.ReadLine());
  369.             var pack = Console.ReadLine();
  370.             //
  371.             var hall = "";
  372.             var price = 0.0;
  373.             var pricePack = 0.0;
  374.  
  375.             //
  376.             if (groupSize <= 120)
  377.             {
  378.                 if (groupSize <= 50)
  379.                 {
  380.                     hall = "Small Hall";
  381.                     price = 2500;
  382.                 }
  383.                 else if (groupSize <= 100)
  384.                 {
  385.                     hall = "Terrace";
  386.                     price = 5000;
  387.                 }
  388.                 else if (groupSize <= 120)
  389.                 {
  390.                     hall = "Great Hall";
  391.                     price = 7500;
  392.                 }
  393.  
  394.                 var total = 0.0;
  395.                 if (pack == "Normal")
  396.                 {
  397.                     pricePack = 500;
  398.                     total = price + pricePack;
  399.                     total *= 0.95;
  400.                 }
  401.                 else if (pack == "Gold")
  402.                 {
  403.                     pricePack = 750;
  404.                     total = price + pricePack;
  405.                     total *= 0.9;
  406.                 }
  407.                 else if (pack == "Platinum")
  408.                 {
  409.                     pricePack = 1000;
  410.                     total = price + pricePack;
  411.                     total *= 0.85;
  412.                 }
  413.                 total /= groupSize;
  414.                 Console.WriteLine($"We can offer you the {hall}");
  415.                 Console.WriteLine($"The price per person is {total:f2}$");
  416.             }
  417.             else
  418.             {
  419.                 Console.WriteLine("We do not have an appropriate hall.");
  420.             }
  421.         }
  422.  
  423.         private static void Choose_Drink_2_0()
  424.         {
  425.             //The {profession} has to pay {totalPrice}.
  426.             var profession = Console.ReadLine();
  427.             var quatity = int.Parse(Console.ReadLine());
  428.             double[] price = { 0.7, 1.0, 1.7, 1.2 };
  429.             var total = 0.0;
  430.             var drink = "";
  431.             if (profession == "Athlete")
  432.             {
  433.                 drink = "Water";
  434.                 total = price[0] * quatity;
  435.             }
  436.             else if (profession == "Businessman" || profession == "Businesswoman")
  437.             {
  438.                 drink = "Coffee";
  439.                 total = price[1] * quatity;
  440.             }
  441.             else if (profession == "SoftUni Student")
  442.             {
  443.                 drink = "Beer";
  444.                 total = price[2] * quatity;
  445.             }
  446.             else
  447.             {
  448.                 drink = "Tea";
  449.                 total = price[3] * quatity;
  450.             }
  451.             Console.WriteLine($"The {profession} has to pay {total:f2}.");
  452.         }
  453.  
  454.         private static void Choose_DRink()
  455.         {
  456.             var profession = Console.ReadLine();
  457.             var drink = "";
  458.             if (profession == "Athlete")
  459.             {
  460.                 drink = "Water";
  461.             }
  462.             else if (profession == "Businessman" || profession == "Businesswoman")
  463.             {
  464.                 drink = "Coffee";
  465.             }
  466.             else if (profession == "SoftUni Student")
  467.             {
  468.                 drink = "Beer";
  469.             }
  470.             else
  471.             {
  472.                 drink = "Tea";
  473.             }
  474.             Console.WriteLine(drink);
  475.         }
  476.     }
  477. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement