Dong_Minh_Doxygen: D:/Google Drive/ODU/Documents/2018/MSIM 408 - Introduction to Game Development/P1/P1_Base(1)/P1_Base/P1_Base/Program.cs Source File

Dong Minh

Program.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 
7 namespace P1_Base
8 {
9  class Program
10  {
29  public static List<int> Factorization(int num)
30  {
31  List<int> result = new List<int>();
32 
33  // Take out the 2s
34  while (num % 2 == 0)
35  {
36  result.Add(2);
37  num /= 2;
38  }
39 
40  // Take out other primes.
41  int factor = 3;
42  while (factor * factor <= num)
43  {
44  if (num % factor == 0)
45  {
46  result.Add(factor);
47  num /= factor;
48  }
49  else factor += 2;
50  }
51 
52  // If num is not 1, then whatever is left is prime
53  if (num > 1)
54  result.Add(num);
55 
56  return result;
57 
58  } // end of Factorization function
59 
76  public static int GCD(int a, int b)
77  {
78  while (a != 0 && b != 0)
79  {
80  if (a > b)
81  a %= b;
82  else
83  b %= a;
84  }
85 
86  return a == 0 ? b : a;
87  }// End GCD
88 
104  public static int LCD(int a, int b)
105  {
106  int num1, num2;
107 
108  if (a > b)
109  {
110  num1 = a;
111  num2 = b;
112  }
113  else
114  {
115  num1 = b;
116  num2 = a;
117  }
118 
119  for (int i = 1; i < num2; i++)
120  {
121  if ((num1 * i) % num2 == 0)
122  return i * num1;
123  }
124  return num1 * num2;
125  }
126 
140  public static int CF(int a, int b)
141  {
142  int i, j, hcf = 1;
143 
144  j = (a < b) ? a : b;
145  for (i = 1; i <= j; i++)
146  if (a % i == 0 && b % i == 0)
147  hcf = i;
148 
149  return hcf;
150  }
151 
163  static void Main(string[] args)
164  {
165  int a = -1, b = -1;
166  string sa, sb;
167  int[] primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
168  47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
169 
170  bool isContinue = true;
171 
172  while (isContinue)
173  {
174  bool aValid = false, bValid = false;
175  Console.WriteLine("Enter the first number:");
176  while (!aValid)
177  {
178  sa = Console.ReadLine();
179  try
180  {
181  a = Int32.Parse(sa);
182  }
183  catch (Exception e)
184  {
185  Console.WriteLine("{0} is not a valid integer.", sa);
186  }
187  if (a < 1 || a > 100)
188  Console.WriteLine("Please enter a number between 1 and 100.");
189  else
190  aValid = true;
191  }
192 
193  Console.WriteLine("Enter the second number:");
194  while (!bValid)
195  {
196  sb = Console.ReadLine();
197  try
198  {
199  b = Int32.Parse(sb);
200  }
201  catch (Exception e)
202  {
203  Console.WriteLine("{0} is not a valid integer.", sb);
204  }
205  if (b < 1 || b > 100)
206  Console.WriteLine("Please enter a number between 1 and 100.");
207  else
208  bValid = true;
209  }
210 
211  // \\
212  // **Enter your code here.** \\
213  // \\
214  Console.WriteLine(""); // to add a space
215 
216  // Get the first factor text and display it
217  Console.WriteLine("The factors of {0} are:", a);
218  var factorsA = string.Join(" ", Factorization(a));
219  Console.WriteLine(factorsA);
220 
221  // Get the second factor text and display it
222  Console.WriteLine("The factors of {0} are:", b);
223  var factorsB = string.Join(" ", Factorization(b));
224  Console.WriteLine(factorsB);
225 
226  // Find the highest common number then factor and display it
227  Console.WriteLine("The common factors of {0} and {1} are:", a, b);
228  int newCommonFactor = CF(a, b);
229  string primeCommonFactors = string.Join(" ", Factorization(newCommonFactor));
230  Console.WriteLine(primeCommonFactors);
231 
232  // Get the GCF and LCD and display it
233  Console.WriteLine("The GCF of {0} and {1} is: {2}", a, b, GCD(a, b));
234  Console.WriteLine("The LCD of {0} and {1} is: {2}", a, b, LCD(a, b));
235 
236  // \\
237  // **End writing own code** \\
238  // \\
239 
240  Console.WriteLine("\nDo you want to continue? Y/N");
241  string newLoop = Console.ReadLine();
242  if (newLoop[0] == 'Y' || newLoop[0] == 'y')
243  {
244  Console.WriteLine();
245  isContinue = true;
246  }
247  else
248  isContinue = false;
249  }
250  }
251  }
252 }
static int GCD(int a, int b)
The greatest common demonitor (GCD) will take in the numbers and will find the GCD value...
Definition: Program.cs:76
static void Main(string[] args)
The main function to run the entire program. This program will ask the user to input two numbers...
Definition: Program.cs:163
static int CF(int a, int b)
The comomn factors will be found when two numbers are input into this
Definition: Program.cs:140
static List< int > Factorization(int num)
The factorization will find the prime numbers, place it in the list and repeat until factorization ca...
Definition: Program.cs:29
static int LCD(int a, int b)
The least common denominator (LCD) will take in the numbers and will find the LCD value...
Definition: Program.cs:104
Generated by   doxygen 1.8.13