Complete the code to print each number in the array using a for loop.
int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine(numbers[i]);
}The for loop should run while i is less than the length of the array to avoid going out of bounds.
Complete the code to print each word in the array using a foreach loop.
string[] words = {"apple", "banana", "cherry"};
foreach ([1] word in words)
{
Console.WriteLine(word);
}int or char causes a type mismatch error.bool is incorrect because the array does not contain boolean values.The array contains strings, so the loop variable must be of type string.
Fix the error in the for loop condition to avoid runtime errors.
double[] values = {2.5, 3.6, 4.7};
for (int index = 0; index < values.Length; index++)
{
Console.WriteLine(values[index]);
}The loop must run while index is less than the array length to prevent accessing invalid indexes.
Fill both blanks to create a dictionary with word lengths for words longer than 3 characters.
string[] words = {"cat", "elephant", "dog", "giraffe"};
var lengths = new Dictionary<string, int>();
foreach (var word in words)
{
if (word.Length > 3)
{
lengths[word] = word.Length;
}
}Count or Count() causes errors because strings do not have these members.Length() with parentheses causes a syntax error.The property Length gives the number of characters in a string. It is used without parentheses.
Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values for words longer than 4 characters.
string[] words = {"tree", "mountain", "river", "sky"};
var result = new Dictionary<string, int>();
foreach (var word in words)
{
if (word.Length > 4)
{
result[word.ToUpper()] = word.Length;
}
}Length() with parentheses causes errors.ToLower() instead of ToUpper() changes the case incorrectly.Use Length property to check word length, ToUpper() method to convert the word to uppercase, and Length property again to get the length value.