legendxs

1.Intro-Programming-Homework solutions

Mar 9th, 2014
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.71 KB | None | 0 0
  1. /* Problem 1.   Play with Visual Studio
  2.  * Familiarize yourself with Microsoft Visual Studio (if you already have it installed) or install Visual Studio
  3.  * (or Visual Studio Express) at your laptop or home computer. Search in Internet for the correct download
  4.  * link: http://google.com/search?q=download+visual+studio.
  5.  * Start Visual Studio and play with it. Create a simple C# program (console application), compile and run it.
  6.  */
  7. using System;
  8.  
  9. public class PrintWindows
  10. {
  11.     public static void Main()
  12.     {
  13.         Console.WriteLine("Windows");
  14.     }
  15. }
  16.  
  17. /* Problem 4.   Hello World
  18.  * Create, compile and run a “Hello C#” console application. Ensure you have named the application well (e.g. “”HelloCSharp”).
  19.  */
  20. using System;
  21.  
  22. public class HelloCSharp
  23. {
  24.     public static void Main()
  25.     {
  26.         Console.WriteLine("HelloCSharp");
  27.     }
  28. }
  29.  
  30. /* Problem 5.   Print Your Name
  31.  * Modify the previous application to print your name. Ensure you have named the application well (e.g. “PrintMyName”).
  32.  */
  33. using System;
  34.  
  35. public class PrintMyName
  36. {
  37.     public static void Main()
  38.     {
  39.         Console.WriteLine("Pesho");
  40.     }
  41. }
  42.  
  43. /* Problem 6.   Print Numbers
  44.  * Write a program to print the numbers 1, 101 and 1001, each at a separate line.
  45.  */
  46. using System;
  47.  
  48. public class PrintNumbers
  49. {
  50.     public static void Main()
  51.     {
  52.         Console.WriteLine(1);
  53.         Console.WriteLine(101);
  54.         Console.WriteLine(1001);
  55.     }
  56. }
  57.  
  58. /* Problem 7.   Print First and Last Name
  59.  * Create console application that prints your first and last name, each at a separate line.
  60.  */
  61. using System;
  62.  
  63. public class PrintMyName
  64. {
  65.     public static void Main()
  66.     {
  67.         Console.WriteLine("Petar");
  68.         Console.WriteLine("Tseperski");
  69.     }
  70. }
  71.  
  72. /* Problem 8.   Square Root
  73.  * Create a console application that calculates and prints the square root of the number 12345.
  74.  * Find in Internet “how to calculate square root in C#”.
  75.  */
  76. using System;
  77.  
  78. public class CalcSqureRoot
  79. {
  80.     public static void Main()
  81.     {
  82.         Console.WriteLine(Math.Sqrt(12345));
  83.     }
  84. }
  85.  
  86. /* Problem 9.   Print a Sequence
  87.  * Write a program that prints the first 10 members of the sequence: 2, -3, 4, -5, 6, -7, ...
  88.  */
  89. using System;
  90.  
  91. public class PrintSequence
  92. {
  93.     public static void Main()
  94.     {
  95.         for (int i = 2; i < 12; i++)
  96.         {
  97.             if (i % 2 == 0)
  98.             {
  99.                 Console.Write(i);
  100.             }
  101.             else
  102.             {
  103.                 Console.Write(-i);
  104.             }
  105.  
  106.             if (i != 11)
  107.             {
  108.                 Console.Write(", ");
  109.             }
  110.         }
  111.  
  112.         Console.WriteLine();
  113.     }
  114. }
  115.  
  116. /* Problem 10.  Reformat C# Code
  117.  * Reformat the following C# code to make it readable according to the C# best practices for code formatting.
  118.  * Change the casing of the identifiers in the code (e.g. use PascalCase for the class name):
  119.  
  120. using
  121.  
  122. System;
  123.  
  124. class hoRRiblEcoDe
  125. {
  126.     static
  127.      void
  128.  
  129.         Main()
  130.     {
  131.         Console.
  132.  
  133.     WriteLine("Hi, I am horribly formatted program"
  134. ); Console.
  135.       WriteLine("Numbers and squares:")
  136. ; for (int i = 0;
  137. i < 10;
  138. i++)
  139.         {
  140.             Console.WriteLine(i +
  141.                 " --> " + i
  142.                 *
  143.                 i);
  144.         }
  145.     }
  146. }
  147.  
  148.  */
  149. using System;
  150.  
  151. public class HorribleCode
  152. {
  153.     public static void Main()
  154.     {
  155.         Console.WriteLine("Hi, I am horribly formatted program");
  156.         Console.WriteLine("Numbers and squares:");
  157.        
  158.         for (int i = 0; i < 10; i++)
  159.         {
  160.             Console.WriteLine(i + " --> " + i * i);
  161.         }
  162.     }
  163. }
  164.  
  165. /* Problem 14.  * Current Date and Time
  166.  * Create a console application that prints the current date and time. Find in Internet how.
  167.  */
  168. using System;
  169.  
  170. public class PrintCurrentDateTime
  171. {
  172.     public static void Main()
  173.     {
  174.         Console.WriteLine(DateTime.Now);
  175.     }
  176. }
  177.  
  178. /*
  179.  * Problem 15.  * Age after 10 Years
  180.  * * Write a program to read your age from the console and print how old you will be after 10 years.
  181.  */
  182. using System;
  183.  
  184. public class PrintFutureAge
  185. {
  186.     public static void Main()
  187.     {
  188.         Console.Write("Input your age: ");
  189.         string input = Console.ReadLine();
  190.         int age = int.Parse(input);
  191.         int result = age + 10;
  192.         Console.WriteLine("Your age after 10 years will be: {0}", result);
  193.     }
  194. }
  195.  
  196. /* Problem 16.  * Print Long Sequence
  197.  * Write a program that prints the first 1000 members of the sequence: 2, -3, 4, -5, 6, -7, …
  198.  * You might need to learn how to use loops (search in Internet).
  199.  */
  200. using System;
  201.  
  202. public class PrintLongSequence
  203. {
  204.     public static void Main()
  205.     {
  206.         for (int i = 2; i < 1002; i++)
  207.         {
  208.             if (i % 2 == 0)
  209.             {
  210.                 Console.Write(i);
  211.             }
  212.             else
  213.             {
  214.                 Console.Write(-i);
  215.             }
  216.  
  217.             if (i != 1001)
  218.             {
  219.                 Console.Write(", ");
  220.             }
  221.         }
  222.  
  223.         Console.WriteLine();
  224.     }
  225. }
  226.  
  227. /* Problem 17.  * Play with the Debugger in Visual Studio
  228.  * Write a program that prints at the console the numbers from 1 to 1000, each at a separate line.
  229.  * You might need to learn how to use loops (search in Internet). Set a breakpoint in the line, which prints the next number
  230.  * in the Visual Studio code editor. Run the program through the debugger using the [F5] key. When the debugger stops at the
  231.  * breakpoint trace the code execution with [F10] key.
  232.  */
  233. using System;
  234.  
  235. public class PrintLongSequenceForDebugging
  236. {
  237.     public static void Main()
  238.     {
  239.         for (int i = 1; i < 1001; i++)
  240.         {
  241.             Console.WriteLine(i);
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment