-3

In C#, how would I go about converting the result of a For Loop into an Array and count (Display on the Console) the number of integers stored in that converted array?

Please see below for what I have thus far:

  for (int i = 1; i < 100; i++)
            {
                if (i % 3 == 0)
                {
                    Console.WriteLine(i);
                }
            }

And feel free to let me know if there is a different way to count the results of the code I have above. Please let me know what you think.

Mark
  • 1
  • 1
  • 1
    Welcome to Stack Overflow! You question is unclear: what do you mean by "result of a for loop", what is that "converted array"? Please write down the expected result. – Klaus Gütter Mar 25 '21 at 18:40
  • Are you allowed to use Linq ? – JHBonarius Mar 25 '21 at 18:42
  • Thank you! Sorry, I am still a beginner. I guess I was thinking that if the results of my For loop written to the the console showed [7,3,5,6,8,9], is there a way to have the console show a count of 6 instead? – Mark Mar 25 '21 at 18:42
  • Yes, @JHBonarius, I am allowed to use Linq. I'm just not sure how I would implement it. – Mark Mar 25 '21 at 18:44
  • 1
    And if you want the amount of numbers divisable by 3 between 1 and 100, just 100/3 and round down. – JHBonarius Mar 25 '21 at 18:44
  • You can [edit] your question to include more details. – Klaus Gütter Mar 25 '21 at 18:44
  • 1
    _"[7,3,5,6,8,9]"_ wait what? The code you show doesn't do that. – JHBonarius Mar 25 '21 at 18:45
  • That is a good point, I could do 100/3 and round down. I was just wondering if there was another way of completing the same thing using a For Loop. In the process of practicing the use of For Loops. – Mark Mar 25 '21 at 18:52
  • @JHBonarius, sorry for any confusion. The statement of "[7,3,5,6,8,9]" was an example outside the result of what my code result actually is. I just didn't want to make my comment to long with the result of my actual code. – Mark Mar 25 '21 at 18:55
  • Use linq, cast your array tolist() then simply convert to anything you may like – redParrot Mar 28 '21 at 19:14

4 Answers4

1

There are a few different ways to accomplish what you're after. You can use a for-loop as you've done and accomplish it like so:

var list = new List<int>();
var sum = 0;
for (var i = 1; i < 100; i++)
{
    if (i % 3 != 0)
        continue;
    
    list.Add(i);
    Console.WriteLine(i);
    sum += i;
}

Console.WriteLine($"Count: {list.Count}");
Console.WriteLine($"Sum: {sum}");

You could also accomplish this with Linq:

var numbers = Enumerable.Range(1, 99)
    .Where(i => i % 3 == 0)
    .ToList();
var sum = numbers.Sum();

numbers.ForEach(Console.WriteLine);
Console.WriteLine($"Count: {numbers.Count}");
Console.WriteLine($"Sum: {sum}");
JD Davis
  • 3,517
  • 4
  • 28
  • 61
0

You can use List for that purpose:

 var list = new List<int>();
 for (int i = 1; i < 100; i++)
 {
     if (i % 3 == 0)
     {
         list.Add(i);
         Console.WriteLine(i);
     }
  }
  Console.WriteLine(list.Count);
Eugene
  • 1,487
  • 10
  • 23
0
List<int> myList = new List<int>();
for (int i = 1; i < 100; i++) {
    if (i % 3 == 0) {
        Console.WriteLine(i);
        myList.Add(i);                    
    }
}
        
Console.WriteLine(myList.Count);

//if you want an array....
int[] myArray;
myArray = myList.ToArray();
Gautham M
  • 4,816
  • 3
  • 15
  • 37
RickH
  • 1
  • 1
0

There is no need to insert the numbers into a collection to count them. Just use a variable as counter

int count = 0;
for (int i = 1; i < 100; i++)
{
    if (i % 3 == 0)
    {
        count++; // Increments count by 1.
        Console.WriteLine(i);
    }
}
Console.WriteLine("The count is " + count);
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188