-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
312 lines (241 loc) · 11.4 KB
/
Program.cs
File metadata and controls
312 lines (241 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
namespace ClassMethod2
{
using System;
using System.Drawing;
using static System.Runtime.InteropServices.JavaScript.JSType;
internal class Program
{
static void Main(string[] args)
{
bool running = true;
while (running)
{
Console.WriteLine("If you want to exit the program enter 'exit' or 'quit'.");
Console.WriteLine("Press any key to enter the program...");
Console.ReadKey();
string command = Console.ReadLine();
// Check if the user wants to exit
if (command.Equals("exit", StringComparison.OrdinalIgnoreCase) ||
command.Equals("quit", StringComparison.OrdinalIgnoreCase))
{
running = false;
break; // Exit the loop
}
//CONSOLE
// 1. Ask for name and favorite color
Console.WriteLine("What is your name:");
string name = Console.ReadLine();
Console.WriteLine("\nWhat is your favorite colour:");
string favColour = Console.ReadLine();
Console.Clear();
// 2. Convert string to ConsoleColor
if (Enum.TryParse(favColour, true, out ConsoleColor color))
{
Console.ForegroundColor = color;
Console.WriteLine($"Hello {name}!");
}
else
{
Console.WriteLine("Invalid color! Using default.");
}
// 3. Ask for two numbers and show sum
Console.WriteLine("\nEnter one digit:");
double num1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another digit:");
double num2 = int.Parse(Console.ReadLine());
Console.WriteLine($"You have entered {num1} and {num2}. The sum of your digits is {num1 + num2}");
//MATH
// 1. square root
Console.WriteLine("\nEnter one digit:");
double num3 = int.Parse(Console.ReadLine());
Console.WriteLine($"You have entered {num3}. The square root of your digits is {Math.Sqrt(num3)}");
// 2. Ask for two numbers and show smallest
Console.WriteLine("\nEnter one digit:");
double num4 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter another digit:");
double num5 = int.Parse(Console.ReadLine());
Console.WriteLine($"Your smallest number is {Math.Min(num4, num5)}");
// 3. Radius of a circle
Console.WriteLine("\nEnter the radius of the circle:");
double radius = double.Parse(Console.ReadLine());
Console.WriteLine($"The area of the circle with radius {radius} is {Math.PI * Math.Pow(radius, 2)}");
//RANDOM
// 1. Generate a random number
Random rand = new Random();
int dice = rand.Next(1, 7); // 1 to 6
Console.WriteLine($"\nThe number on the dice is {dice}");
// 2. Simple lottery simulation
int secretNumber = rand.Next(1, 11); // 1 to 10
Console.WriteLine("Guess a number between 1 and 10:");
int guess = int.Parse(Console.ReadLine());
if (guess == secretNumber)
Console.WriteLine("You guessed it!");
else
Console.WriteLine($"Wrong! The number was {secretNumber}");
// 3. Generate three random colors (using RGB values)
for (int i = 1; i <= 3; i++)
{
int r = rand.Next(0, 256); // Red component (0-255)
int g = rand.Next(0, 256); // Green component (0-255)
int b = rand.Next(0, 256); // Blue component (0-255)
Color randomColor = Color.FromArgb(r, g, b);
Console.WriteLine($"Color {i}: R={r}, G={g}, B={b}");
}
//DateTime
//1. Birthdate
Console.WriteLine("Enter your birthdate (yyyy-MM-dd):");
string input = Console.ReadLine();
// Parse the input into a DateTime object
if (DateTime.TryParse(input, out DateTime birthdate))
{
DateTime today = DateTime.Today;
// Calculate age
int age = today.Year - birthdate.Year;
// If birthday hasn't occurred yet this year, subtract 1
if (birthdate.Date > today.AddYears(-age))
{
age--;
}
Console.WriteLine($"You are {age} years old.");
}
else
{
Console.WriteLine("Invalid date format. Please use yyyy-MM-dd.");
}
//2. Day of the week
string filePath = "event.txt"; // File to store the even
// Check if an event was saved before
if (File.Exists(filePath))
{
string savedEvent = File.ReadAllText(filePath);
Console.WriteLine($"Reminder: Your last event was - {savedEvent}");
}
// Get today's day of the week
DateTime today = DateTime.Today;
DayOfWeek dayOfWeek = today.DayOfWeek;
Console.WriteLine($"Today is {dayOfWeek}, enter your event for today:");
string e = Console.ReadLine();
// Save the event to file
File.WriteAllText(filePath, e);
Console.WriteLine("Your event has been saved. Run the program again to be reminded!");
// 3. Difference in days between today and the next holiday
// Get today's date
Console.WriteLine("Enter the date of your next holiday (yyyy-mm-dd):");
string yourDay = Console.ReadLine();
// Try to parse the yourDay
if (DateTime.TryParse(yourDay, out DateTime holiday))
{
if (holiday < today)
{
Console.WriteLine("That holiday has already passed!");
}
else
{
// Calculate difference
TimeSpan difference = holiday - today;
Console.WriteLine($"Your holiday is in {difference.Days} day(s).");
}
}
else
{
Console.WriteLine("Invalid date format. Please enter in yyyy-mm-dd format.");
}
// STRING
// 1. reverse order
Console.WriteLine("Enter any sentence: ");
string sentence = Console.ReadLine();
// Convert string to char array and reverse
char[] chars = sentence.ToCharArray();
Array.Reverse(chars);
// Create reversed string
string reversed = new string(chars);
Console.WriteLine("Reversed sentence:");
Console.WriteLine(reversed);
// 2. palindrome
// Normalize input (remove spaces, make lowercase)
string cleaned = sentence.Replace(" ", "").ToLower();
// Reverse the string
char[] chars2 = cleaned.ToCharArray();
Array.Reverse(chars);
string reversed2 = new string(chars2);
// Compare original and reversed
if (cleaned == reversed2)
{
Console.WriteLine("It is a palindrome!");
}
else
{
Console.WriteLine("It is not a palindrome.");
}
// 3. how many times a specific word appears
Console.WriteLine("Enter a paragraph: ");
string paragraph = Console.ReadLine();
Console.WriteLine("Enter the word to count:");
string wordToCount = Console.ReadLine();
// Normalize case for case-insensitive comparison
string[] words = paragraph.Split(new char[] { ' ', '.', ',', '!', '?', ';', ':', '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
int count = 0;
foreach (string word in words)
{
if (word.Equals(wordToCount, StringComparison.OrdinalIgnoreCase))
{
count++;
}
}
Console.WriteLine($"The word \"{wordToCount}\" appears {count} time(s) in the paragraph.");
// ARRAY
// 1. reverse order in array
int[] numbers = new int[5];
Console.WriteLine("Enter any 5 numbers:");
for (int i = 0; i < numbers.Length; i++)
{
Console.Write($"Number {i + 1}: ");
numbers[i] = int.Parse(Console.ReadLine());
}
// Print numbers in reverse order
Console.WriteLine("\nNumbers in reverse order:");
for (int i = numbers.Length - 1; i >= 0; i--)
{
Console.WriteLine(numbers[i]);
}
// 2. convert string into a character array and count how many vowels are in the string
Console.Write("Enter a word: ");
string str = Console.ReadLine();
char[] characters = str.ToCharArray();
int vowelCount = 0;
foreach (char c in characters)
{
char lowerChar = Char.ToLower(c);
if (lowerChar == 'a' || lowerChar == 'e' || lowerChar == 'i' || lowerChar == 'o' || lowerChar == 'u')
{
vowelCount++;
}
}
Console.WriteLine($"The string contains {vowelCount} vowels.");
// 3. Create a program where the user can continually add names
List<string> names = new List<string>();
string input2;
Console.WriteLine("Enter names. Type 'stop' when you want to finish.");
// Loop until the user types "stop"
while (true)
{
Console.Write("Enter a name: ");
input2 = Console.ReadLine();
// Check if the user wants to stop
if (input2.ToLower() == "stop")
break;
// Add the name to the list
names.Add(input2);
}
// Display all names entered
Console.WriteLine("\nNames entered:");
foreach (string name2 in names)
{
Console.WriteLine(name2);
}
// Display the total count
Console.WriteLine($"\nTotal number of names entered: {names.Count}");
}
}
}
}