avitoholix

ConsolInputOutputHW

Jun 20th, 2014
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.98 KB | None | 0 0
  1. //1. Sum of 3 Numbers
  2.  
  3. using System;
  4.  
  5. //Write a program that reads 3 real numbers from the console and prints their sum. Examples:
  6.  
  7. //a     b    c      sum
  8. //3     4    11       18
  9. //-2    0    3        1
  10. //5.5   4.5 20.1     30.1
  11.  
  12. class Program
  13. {
  14.     static void Main()
  15.     {
  16.         double a = double.Parse(Console.ReadLine());
  17.         double b = double.Parse(Console.ReadLine());
  18.         double c= double.Parse(Console.ReadLine());
  19.         Console.WriteLine(a+b+c);
  20.     }
  21. }
  22.  
  23. //2. Print Company Information
  24.  
  25. using System;
  26.  
  27. //A company has name, address, phone number, fax number, web site and manager. The manager has first name,
  28. //last name, age and a phone number. Write a program that reads the information about a company and its manager
  29. //and prints it back on the console.
  30.  
  31. //program user
  32.  
  33. //Company name:       Software University
  34. //Company address:     26 V. Kanchev, Sofia
  35. //Phone number:        +359 899 55 55 92
  36. //Fax number:
  37. //Web site:          http://softuni.bg
  38. //Manager first name:    Svetlin
  39. //Manager last name:     Nakov
  40. //Manager age:              25
  41. //Manager phone:      +359 2 981 981
  42. //
  43. //Software University
  44. //Address:      26 V. Kanchev, Sofia
  45. //Tel. +359 899 55 55 92
  46. //Fax: (no fax)
  47. //Web site: http://softuni.bg
  48. //Manager: Svetlin Nakov (age:
  49. //25, tel. +359 2 981 981)
  50.  
  51. class Program
  52. {
  53.     static void Main()
  54.     {
  55.         string[] infoList = new string[9] { "Company name: ", "Company addreess: ", "Phone number: ", "Fax number: ",
  56.                                                 "Web site: ", "Manager first name: ", "Manager last name: ", "Manager age:",
  57.                                                 "Manager phone: " };
  58.         string[] infoFromUser = new string[9];
  59.         for (int i = 0; i < infoList.Length; i++)
  60.         {
  61.             Console.Write(infoList[i]);
  62.             infoFromUser[i] = Console.ReadLine();
  63.         }
  64.         Console.WriteLine("------------------------------");
  65.         string[] infoPrint = new string[8] { "", "Address: ", "Tel. ", "Fax: (no fax)", "Web site: ", "Manager: ", "age.", "tel. "};
  66.         for (int i = 0; i < infoPrint.Length-2; i++)
  67.         {
  68.             Console.WriteLine();
  69.             Console.Write(infoPrint[i]+infoFromUser[i]);
  70.         }
  71.             Console.Write(" "+infoFromUser[6]+"( "+infoPrint[6]+"\n"+infoFromUser[7]+" "+infoPrint[7]+infoFromUser[8]+")");
  72.             Console.WriteLine();  
  73.     }
  74. }
  75.  
  76. // 3. Circle Perimeter and Area
  77.  
  78. //Write a program that reads the radius r of a circle and prints its perimeter and area formatted with 2 digits after the
  79. //decimal point. Examples:
  80. //r   perimeter area
  81. //2   12.57      12.57
  82. //3.5 21.99      38.48
  83.  
  84. class Program
  85. {
  86.     static void Main()
  87.     {
  88.         //A = Pi * r^2
  89.         //P = 2 * Pi * r
  90.         Console.WriteLine("Write your radios...");
  91.         double r = double.Parse(Console.ReadLine());
  92.         double perimeter = 2 * Math.PI * r;
  93.         double area = Math.PI * r * r;
  94.         Console.WriteLine("Perimetar = {0}, Area = {1}",perimeter,area);
  95.     }
  96. }
  97.  
  98. // 4. Number Comparer
  99.  
  100. using System;
  101.  
  102. //Write a program that gets two numbers from the console and prints the greater of them. Try to implement this
  103. //without if statements. Examples:
  104.  
  105. //a       b     greater
  106. //5       6        6
  107. //10      5        10
  108. //0       0         0
  109. //-5     -2        -2
  110. //1.5     1.6       1.6
  111.  
  112. class Program
  113. {
  114.     static void Main()
  115.     {
  116.         int a = int.Parse(Console.ReadLine());
  117.         int b = int.Parse(Console.ReadLine());
  118.         if (a > b)
  119.         {
  120.             Console.WriteLine(a);
  121.         }
  122.         else if (a < b)
  123.         {
  124.             Console.WriteLine(b);
  125.         }
  126.         else if (a == b)
  127.         {
  128.             Console.WriteLine(a);
  129.         }
  130.     }
  131. }
  132.  
  133. //5. Formatting Numbers
  134.  
  135. floating-point c and prints
  136. //them in 4 virtual columns on the console. Each column should have a width of 10 characters. The number a should
  137. //be printed in hexadecimal, left aligned; then the number a should be printed in binary form, padded with zeroes,
  138. //then the number b should be printed with 2 digits after the decimal point, right aligned; the number c should be
  139. //printed with 3 digits after the decimal point, left aligned. Examples:
  140.  
  141. //a            b              c                       result
  142. //254        11.6            0.5              |FE |0011111110| 11.60|0.500 |
  143. //499       -0.5559         10000             |1F3 |0111110011| -0.56|10000 |
  144. //0           3            -0.1234            |0 |0000000000| 3|-0.123
  145.  
  146. class Program
  147. {
  148.     static void Main()
  149.     {
  150.         int a = int.Parse(Console.ReadLine());
  151.         double b = double.Parse(Console.ReadLine());
  152.         double c = double.Parse(Console.ReadLine());
  153.         string myHexNumber = a.ToString("X");
  154.         string myBinary = Convert.ToString(a, 2);
  155.         Console.WriteLine("|{0,-10}|{1,10:D10}|{2,10:F2}|{3,-10:F3}|",myHexNumber,myBinary,b,c);
  156.     }
  157. }
  158.  
  159. //6. Quadratic Equation
  160.  
  161. using System;
  162.  
  163. //Write a program that reads the coefficients a, b and c of a quadratic equation ax2 + bx + c = 0 and solves it (prints
  164. //its real roots). Examples:
  165.  
  166. //a           b          c             roots
  167. //2           5         -3           x1=-3; x2=0.5
  168. //-1          3          0           x1=3; x2=0
  169. //-0.5        4         -8           x1=x2=4
  170. //5           2          8         no real roots
  171.  
  172. class Program
  173. {
  174.     static void Main()
  175.     {
  176.         //double sqrtpart = b * b - 4 * a * c;
  177.         //answer = (b + Math.Sqrt(sqrtpart)) / 2 * a;
  178.         //textBox4.Text = answer.ToString();
  179.     }
  180. }
  181.  
  182. //7. Sum of 5 Numbers
  183.  
  184. using System;
  185.  
  186. //Write a program that enters 5 numbers (given in a single line, separated by a space), calculates and prints their
  187. //sum. Examples:
  188.  
  189. //numbers       sum      numbers      sum       numbers            sum
  190. //1 2 3 4 5      15   10 10 10 10 10   50    1.5 3.14 8.2 -1 0    11.84
  191.  
  192. class Program
  193. {
  194.     static void Main()
  195.     {
  196.         double[] numbersStrem = new double[5];
  197.         double result = 0;
  198.         for (int i = 0; i < numbersStrem.Length; i++)
  199.         {
  200.             numbersStrem[i] = double.Parse(Console.ReadLine());
  201.              result += numbersStrem[i];
  202.  
  203.         }
  204.         Console.WriteLine(result);
  205.  
  206.     }
  207. }
  208.  
  209. //8. Numbers from 1 to n
  210.  
  211. using System;
  212.  
  213. //Write a program that reads an integer number n from the console and prints all the numbers in the interval [1..n],
  214. //each on a single line. Note that you may need to use a for-loop. Examples:
  215. //numbers      sum     numbers      sum       numbers      sum
  216. //3               1                  
  217. //                2
  218. //                3
  219.  
  220. class Program
  221. {
  222.     static void Main()
  223.     {
  224.         int n = int.Parse(Console.ReadLine());
  225.         for (int i = 1; i <= n; i++)
  226.         {
  227.             Console.WriteLine(i);
  228.         }
  229.     }
  230. }
  231.  
  232.  
  233. //9. Sum of n Numbers
  234.  
  235. using System;
  236.  
  237. //Write a program that enters a number n and after that enters more n numbers and calculates and prints their sum.
  238. //Note that you may need to use a for-loop. Examples:
  239. //numbers sum numbers sum numbers sum
  240.  
  241. //3
  242. //20
  243. //60
  244. //10
  245. //90 5
  246. //2
  247. //6.5 1
  248. //1
  249. //-1
  250. //-0.5
  251. //4
  252. //2
  253.  
  254. class Program
  255. {
  256.     static void Main()
  257.     {
  258.         int n = int.Parse(Console.ReadLine());
  259.         double sum = 0;
  260.         double[] arr = new double[n];
  261.         for (int i = 0; i < arr.Length; i++)
  262.         {
  263.             arr[i] = double.Parse(Console.ReadLine());
  264.             sum += arr[i];
  265.         }
  266.         Console.WriteLine(sum);
  267.         Console.WriteLine();
  268.     }
  269. }
  270.  
  271. //10. Fibonacci Numbers
  272.  
  273. sing System;
  274.  
  275. class Program
  276. {
  277.     static void Main()
  278.     {
  279.         int n = int.Parse(Console.ReadLine());
  280.         int x = 1;
  281.         int y = 0;
  282.         int z = 0;
  283.         int counter = 0;
  284.         for (int i = 0; i < n; i++)
  285.         {
  286.             counter++;
  287.             z = x + y;
  288.             x = y;
  289.             y = z;
  290.             z = x + y;
  291.             Console.WriteLine(z);
  292.         }
  293.         Console.WriteLine("Item: " + counter);
  294.     }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment