✍️ Practice (Easy)
1. Some Cool Ints
Create an array containing the integers 1 through 10. Write it in as many different ways as you can (aim for at least three).
💡Solution
// 1. Array initializer
int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 2. With new
int[] b = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 3. With an explicit size
int[] c = new int[10] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// 4. Declare first, assign later
int[] d;
d = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
2. W3Schools Syntax Practice
Complete these Exercises
3. Party Guests
Here's the guest list for a party:
string[] guests = {"poggie", "preeti pearl", "youmna"};
Write a program that asks the user for their name, then tells them whether they're on the guest list.
👀Hint
The check is case insensitive, so Helen and helen do not count as a match.
💡Solution
string[] guests = {"poggie", "preeti pearl", "youmna"};
Console.Write("What's your name? ");
string name = Console.ReadLine();
bool isInvited = false;
foreach (string guest in guests)
{
if (guest == name)
{
isInvited = true;
break;
}
}
if (isInvited)
{
Console.WriteLine($"Welcome {name} <3");
}
else
{
Console.WriteLine($"gtfo {name}");
}
4. Some Cool Grades
Write a program that:
- Prompts the user for their 3 test grades, one at a time
- Stores all the grades in a single array
- Prints the array
👀Hint
How do you prompt the user in C#?
💡Solution
double[] grades = new double[3];
// Parse from User
for (int i = 0; i < grades.Length; i++)
{
Console.Write($"Enter grade: ");
double grade = double.Parse(Console.ReadLine());
grades[i] = grade;
}
// Print Result
Console.Write($"Your grades: ");
foreach (double grade in grades)
{
Console.Write($"{grade}, ");
}
5. Sumthings Going On
Using the array below, calculate the total sum of each number squared
int[] nums = {1, 7, 2, -3, 5, 9};
💡Solution
int[] nums = { 1, 7, 2, -3, 5, 9 };
int total = 0;
foreach (int num in nums)
{
total += num * num;
}
Console.WriteLine(total);