FAQ: Loops - Review

Solution for the second problem:

using System;

namespace LoopsReview
{
  class Program
  {
    static void Main(string[] args)
    {
     //Declaring an array which hold numbers
     int[] numbers = { 10, 5, 12, 3421, 904, 84 };

     //Calling every numbers of the array via loop
     for (int i = 0; i < numbers.Length; i++)
     {
     //Checking if it's odd or not
     if (numbers[i] % 2 == 0)
        Console.WriteLine($"{numbers[i]} is an even number.");
     else
        Console.WriteLine($"{numbers[i]} is an odd number.");
     }
    }
  }
}

Second problem:

int[] numbers = new int[] {1,2,3,4,76,35,34,53,5,34,87,75,665};

    foreach(int number in numbers)

    {

      if(number % 2 == 0)

      {

        Console.WriteLine($"{number} is even");

      }

      else 

      {

         Console.WriteLine($"{number} is odd");

      }

    }

Here’s my attempt. I decided to also list the factors for each number and through that, work out if the number is a prime.

for (int i = 1; i <= 50; ++i)
{
    Console.WriteLine(i % 2 == 0 ? $"{i}: even": $"{i}: odd") ;
    // List factors or mark as prime
    string factors = "";
    for (int j = 1; j<i/2+1; ++j)
    {
        if (i % j == 0)
        {
factors = factors + $" {j}";
        }
    }
    if (i < 4) { factors = " 1"; };
    Console.WriteLine(factors == " 1" ? $"{i} is a prime number! \n": $"The factors of {i} are{factors}\n");

Second challenge. I confess that if I hadn’t peaked here and seen the split function, I’d have been stuck. With that, it was pretty straightforward. I made life difficult for myself as I wanted to get rid of the extra punctuation. This cost me the most time. I couldn’t find a Regex.Replace formula that would would go through the final string and take out the punctuation, but not the spaces. Any tips here appreciated. The formula I did use, I just found by googling, but it would be more efficient were it outside of the loop.

//String generated using "=rand(3)" in MS Word
string input = "Video provides a powerful way to help you prove your point. When you click Online Video, you can paste in ... (shortened here for space!)");
char wantedLetter = (char)Console.Read();
wantedLetter = char.ToLower(wantedLetter);
string output = "";
string[] indivWords = input.Split(' ');
foreach (string word in indivWords)
{
    if (word.Substring(0, 1).ToLower() == char.ToString(wantedLetter))
    {
        string subword = Regex.Replace(word, "[^a-zA-Z0-9]+", "", RegexOptions.Compiled);
        output = output + " " + subword;
    }
}
Console.WriteLine(output == "" ? $"Sorry, no words starting with {wantedLetter}" : output);

Why does this code run into an error:

using System; namespace LoopsReview { class Program { static void Main(string[] args) { /* use this space to write your own short program! Here's what you learned: while loop: while(){..} do...while loop: do{...}while(); for loop: for(int i=0; i<x; i++){} foreach loop: foreach(int item in list){} jump statements: break, continue, return Good luck! */ foreach (int item in int[] items = {1, 2, 3}) { Console.WriteLine(item); } } } }

?