|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 |
|
3 | 4 | namespace Checkpoint1 |
4 | 5 | { |
5 | 6 | class Program |
6 | 7 | { |
| 8 | + public static bool end = false; |
| 9 | + |
7 | 10 | static void Main(string[] args) |
8 | 11 | { |
9 | | - Console.WriteLine("Hello World!"); |
| 12 | + // Create 5 different Modules with the functionality below for a user to access via the console |
| 13 | + // Module 1 - counts numbers between 1 and 100 and display the numbers evenly divisible by three |
| 14 | + // Module 2 - ask user to enter a number, then again or "ok" to end the entry. Upon "ok" all numbers are summed and displayed |
| 15 | + // Module 3 - ask user to enter a number. return and display the factorial as "var! = var factorial" |
| 16 | + // Module 4 - computer generate a random number between 1 and 10. Ask user to guess the number. Correct guess = "You win/loose". User has 4 chances |
| 17 | + // Module 5 = ask user to enter a series of numbers separated by commas. Find the largest number and display it to the console. |
| 18 | + |
| 19 | + // Allow user to choose which Module to execute. Continue asking until user chooses any invalid option |
| 20 | + do { |
| 21 | + Console.WriteLine("Execute one of the following by entering the cooresponding number"); |
| 22 | + Console.WriteLine("Enter 1 to display all numbers from 1-100 divisible by three"); |
| 23 | + Console.WriteLine("Enter 2 to get the sum of a series of numbers you enter"); |
| 24 | + Console.WriteLine("Enter 3 to get the factorial of a number you enter"); |
| 25 | + Console.WriteLine("Enter 4 to try and guess the computer chosen number between 1 and 10"); |
| 26 | + Console.WriteLine("Enter 5 to find the largest number in a series of numbers you enter"); |
| 27 | + Console.WriteLine("Any other choice to exit"); |
| 28 | + int choice = int.Parse(Console.ReadLine()); |
| 29 | + |
| 30 | + if (choice == 1) { |
| 31 | + NumbersbyThree(); |
| 32 | + } |
| 33 | + else if (choice == 2) { |
| 34 | + SumofNumbers(); |
| 35 | + } |
| 36 | + else if (choice == 3) { |
| 37 | + Factorial(); |
| 38 | + } |
| 39 | + else if (choice == 4) { |
| 40 | + GuessaNumber(); |
| 41 | + } |
| 42 | + else if (choice == 5) { |
| 43 | + MaxNumber(); |
| 44 | + } |
| 45 | + else { |
| 46 | + Console.WriteLine("Ending Exercise"); |
| 47 | + end = true; |
| 48 | + } |
| 49 | + } while (!EndExercise() == true ); |
| 50 | + // leave this command at the end so your program does not close automatically |
| 51 | + Console.ReadLine(); |
| 52 | + } |
| 53 | + |
| 54 | + // Module 1 - counts numbers between 1 and 100 and display the numbers evenly divisible by three |
| 55 | + public static void NumbersbyThree() |
| 56 | + { |
| 57 | + Console.WriteLine("Numbers by Three"); |
| 58 | + List<int> threeList = new List<int>(); |
| 59 | + for (int i = 1; i <= 100; i++) |
| 60 | + if ( (i - ((i / 3) * 3)) == 0) { |
| 61 | + Console.WriteLine(i); |
| 62 | + threeList.Add(i); |
| 63 | + } |
| 64 | + Console.WriteLine("Count of numbers divisible by 3: " +threeList.Count); |
| 65 | + } |
| 66 | + |
| 67 | + // Module 2 - ask user to enter a number, then again or "ok" to end the entry. Upon "ok" all numbers are summed and displayed |
| 68 | + public static void SumofNumbers() |
| 69 | + { |
| 70 | + Console.WriteLine("Sum of Numbers"); |
| 71 | + int sum1 = 0; |
| 72 | + bool endSum = false; |
| 73 | + do { |
| 74 | + // ask user to input a number or "ok" to end |
| 75 | + Console.WriteLine("Enter a number or type \"ok\" when finished"); |
| 76 | + string userInput = (Console.ReadLine().ToLower()); |
| 77 | + // check to see if user enters "ok" |
| 78 | + if (userInput == "ok") { |
| 79 | + endSum = true; |
| 80 | + } |
| 81 | + // if user enters another number add it to the other numbers |
| 82 | + else sum1 = sum1 + int.Parse(userInput); |
| 83 | + } while (endSum != true); |
| 84 | + // if user has finished entering numbers print the sum of the numbers to the console |
| 85 | + Console.WriteLine("Sum of Numbers: " + sum1); |
10 | 86 | } |
| 87 | + |
| 88 | + // Module 3 - ask user to enter a number. return and display the factorial as "var! = var factorial" |
| 89 | + public static void Factorial() |
| 90 | + { |
| 91 | + Console.WriteLine("Enter a number to find its factorial: "); |
| 92 | + int f1 = int.Parse(Console.ReadLine()); |
| 93 | + int origNum = f1; |
| 94 | + for (int i = f1 - 1; i >= 1; i--){ |
| 95 | + f1 = f1 * i; |
| 96 | + } |
| 97 | + Console.WriteLine(origNum +"! = " +f1); |
| 98 | + } |
| 99 | + |
| 100 | + // Module 4 - computer generate a random number between 1 and 10. Ask user to guess the number. Correct guess = "You win/loose". User has 4 chances |
| 101 | + public static void GuessaNumber() |
| 102 | + { |
| 103 | + // ask user to enter a number. save the number. |
| 104 | + Console.WriteLine("Can you guess the computers number in four tries? Enter a number between 1 and 10: "); |
| 105 | + int userGuess = int.Parse(Console.ReadLine()); |
| 106 | + // computer generate a random number between 1 and 10. save the number. |
| 107 | + Random rnd = new Random(); |
| 108 | + int compGuess = rnd.Next(1, 10); |
| 109 | + bool winCheck = false; |
| 110 | + bool countCheck = false; |
| 111 | + int count1 = 1; |
| 112 | + // check if user number matches computer number OR if user has used four tries |
| 113 | + while (winCheck == false && countCheck == false){ |
| 114 | + count1 = count1 + 1; |
| 115 | + // if user number matches computer number write "you win" to the console. exit exercise. |
| 116 | + if (userGuess == compGuess) { |
| 117 | + winCheck = true; |
| 118 | + Console.WriteLine("They Match. You WIN!"); |
| 119 | + } |
| 120 | + // if user number does not match computer number ask user for another number |
| 121 | + else { |
| 122 | + Console.WriteLine("No Match. Enter a different number: "); |
| 123 | + userGuess = int.Parse(Console.ReadLine()); |
| 124 | + // if user has used all four tries without matching the computer, write "you loose" to the console. exit exercise. |
| 125 | + if (count1 >=4) { |
| 126 | + countCheck = true; |
| 127 | + Console.WriteLine("Four tries. No Match. You Loose."); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + // Module 5 = ask user to enter a series of numbers separated by commas. Find the largest number and display it to the console. |
| 135 | + public static void MaxNumber() |
| 136 | + { |
| 137 | + // Ask user for a series of integers separated by commas. save the input. |
| 138 | + Console.WriteLine("Enter a series of integers separated by commas. Cumputer will return the largest number: "); |
| 139 | + string all = Console.ReadLine(); |
| 140 | + string[] Section = all.Split(","); |
| 141 | + List<int> numList = new List<int>(); |
| 142 | + int eachNum; |
| 143 | + int largest = 0; |
| 144 | + // Separate the string using the "," and save as a list of integers |
| 145 | + foreach(string eachSect in Section){ |
| 146 | + if(Int32.TryParse(eachSect, out eachNum)) |
| 147 | + numList.Add(eachNum); |
| 148 | + } |
| 149 | + // Find the largest integer in the list and save it |
| 150 | + foreach (int num in numList){ |
| 151 | + if (num > largest) { |
| 152 | + largest = num; |
| 153 | + } |
| 154 | + } |
| 155 | + // Print largest number to screen |
| 156 | + Console.WriteLine ("Max Number is: " +largest); |
| 157 | + } |
| 158 | + |
| 159 | + // Check for end of exercise |
| 160 | + public static bool EndExercise() |
| 161 | + { |
| 162 | + return end; |
| 163 | + |
| 164 | + } |
11 | 165 | } |
12 | 166 | } |
0 commit comments