-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
262 lines (220 loc) · 6.43 KB
/
Program.cs
File metadata and controls
262 lines (220 loc) · 6.43 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
// PART 1 -- "HELLO WORLD" & STRING
// Hello World
Console.WriteLine("Hello, World!");
// Console.WriteLine("Text" + Var); & Console.WriteLine($"Text{}");
string aFriend = "Bill";
Console.WriteLine(aFriend);
// "$" called string interpolation
aFriend = "Maria";
Console.WriteLine("Hello " + aFriend);
Console.WriteLine($"Hello {aFriend}");
string firstFriend = "Agus";
string secondFriend = "Budi";
Console.WriteLine($"My Friends are {firstFriend} and {secondFriend}");
Console.WriteLine("My Friends are " + firstFriend + " and " + secondFriend);
// String Length >> .Length
Console.WriteLine($"The name {firstFriend} has letters {firstFriend.Length}");
Console.WriteLine($"The name {secondFriend} has letters {secondFriend.Length}");
// TrimStart(), TrimEnd(), Trim()
string greeting = " Hello World ";
Console.WriteLine($"[{greeting}]");
Console.WriteLine($"[{greeting.Trim()}]");
string trimStartGreeting = greeting.TrimStart();
Console.WriteLine($"[{trimStartGreeting}]");
string trimEndGreeting = greeting.TrimEnd();
Console.WriteLine($"[{trimEndGreeting}]");
string trimGreeting = greeting.Trim();
Console.WriteLine($"[{trimGreeting}]");
// Replace
string friends = $"my friends are {firstFriend} and {secondFriend}";
Console.WriteLine(friends);
Console.WriteLine(friends.Replace("Agus", "Ujang"));
Console.WriteLine(friends);
Console.WriteLine(friends.Replace(secondFriend, "Ujang"));
Console.WriteLine(friends.Replace(secondFriend, firstFriend));
Console.WriteLine($"my friends are {firstFriend}, {secondFriend}, and {secondFriend}".Replace(secondFriend, firstFriend));
string sayHello = "Hello World";
Console.WriteLine(sayHello);
Console.WriteLine(sayHello.Replace("Hello", "Hi"));
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);
// all Caps & all Lower
Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());
// Search Strings
string songLyrics = "you say goodbye and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("Greetings"));
// StartsWith , EndsWith
Console.WriteLine(songLyrics.StartsWith("you"));
Console.WriteLine(songLyrics.EndsWith("hello"));
Console.WriteLine($"songLyrics end with 'goodbye' = {songLyrics.EndsWith("goodbye")}");
// PART 2 -- NUMBERS
WorkWithIntegers();
OrderPrecedence();
Modulo();
Precisions();
MaxMin();
workWithDoubles();
workWithDecimals();
int a = 2100000000;
int b = 2100000000;
// long c = checked(a + b);
// Console.WriteLine(c);
long c = (long)a + (long)b;
Console.WriteLine(c);
void WorkWithIntegers()
{
int a = 18;
int b = 6;
int c = a + b;
Console.WriteLine(c);
// substract
c = a - b;
Console.WriteLine(c);
// multiplication
c = a * b;
Console.WriteLine(c);
// division
Console.WriteLine(a / b);
}
void OrderPrecedence()
{
int a = 5;
int b = 4;
int c = 2;
Console.WriteLine(a + b * c);
Console.WriteLine((a + b) * c);
Console.WriteLine((a + b) / c);
}
void Modulo()
{
int a = 7;
int b = 4;
int c = 3;
int d = (a + b) / c;
int e = (a + b) % c;
Console.WriteLine($"quotient: {d}");
Console.WriteLine($"remainder: {e}");
}
void Precisions()
{
double a = 42.1;
float b = 38.2f;
double c = a + b;
Console.WriteLine(c);
decimal d = 42.1m;
decimal e = 38.2m;
decimal f = d + e;
Console.WriteLine($"{d} + {e} = {f}");
}
void MaxMin()
{
int max = int.MaxValue;
int min = int.MinValue;
int max2 = max + max;
Console.WriteLine($"The range of integers is {min} to {max}");
Console.WriteLine($"what if.. max + 1 = {max + 2}");
Console.WriteLine($"what if.. max + max = {max2}");
Console.WriteLine($"what if.. max + min = {max + min}");
Console.WriteLine($"what if.. max - min = {max - min}");
// max + 1 = min + 0
// max + 2 = min + 1
// max - min = max + max + 1
}
void workWithDoubles()
{
double dMax = double.MaxValue;
double dMin = double.MinValue;
Console.WriteLine($"The range of double is {dMin} to {dMax}");
double third = 1.0 / 3.0;
Console.WriteLine(third);
}
void workWithDecimals()
{
decimal min = decimal.MinValue;
decimal max = decimal.MaxValue;
Console.WriteLine($"The range of the decimal type is {min} to {max}");
double a = 1.0;
double b = 3.0;
Console.WriteLine(a / b);
decimal c = 1.0M;
decimal d = 3.0M;
Console.WriteLine(c / d);
}
// PART 3 -- BRANCH AND LOOPS
ExploreIf();
WhileLoops();
doWhile();
forLoops();
void ExploreIf()
{
int a = 5;
int b = 3;
bool myTest = a + b > 10; // will be true or false
if (myTest) // if true / myTest == 1
{
Console.WriteLine($"{a + b} is greater than 10");
}
else
{
Console.WriteLine($"{a + b} is not greater than 10");
}
int c = 4;
if ((a + b + c > 10) && (a > b))
{
Console.WriteLine($"{a + b + c} is greater than 10");
Console.WriteLine($"And the first number({a}) is greater than the second({b})");
}
else
{
Console.WriteLine($"{a + b + c} is not greater than 10");
Console.WriteLine($"Or the first number({a}) is not greater than the second({b})");
}
if ((a + b + c > 10) || (a > b))
{
Console.WriteLine($"{a + b + c} is greater than 10");
Console.WriteLine($"Or the first number({a}) is greater than the second({b})");
}
else
{
Console.WriteLine($"{a + b + c} is not greater than 10");
Console.WriteLine($"And the first number({a}) is not greater than the second({b})");
}
}
void WhileLoops()
{
int counter = 0;
while (counter < 5)
{
counter++; // print 1 - 5
Console.WriteLine(counter);
//counter++; print 0 - 4
}
}
void doWhile()
{
int counter = 0;
do
{
Console.WriteLine(counter);
counter++;
} while (counter < 5);
}
void forLoops()
{
int length = 5;
for (int i = 0; i < length; i++)
{
if (i == 3)
Console.WriteLine(i);
}
for (int row = 1; row < 11; row++)
{
for (char column = 'a'; column < 'k'; column++)
{
Console.WriteLine($"The cell is ({row}, {column})");
}
}
}
// shift + alt + F to format the code