1
The code is simple and is working perfectly, but wanted tips on how to optimize it, what would be the best methods to use to allocate less memory, best practices, etc.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
List<int> numeros = new List<int>();
List<int> pares = new List<int>();
List<int> impares = new List<int>();
Random random = new Random();
for(int i = 0; i < 20; i++)
{
int n = random.Next(1, 99);
numeros.Add(n);
}
foreach (int item in numeros)
{
if (item % 2 == 0)
pares.Add(item);
else
impares.Add(item);
}
Console.WriteLine("Todos os números:");
int index = 0;
foreach (int item in numeros)
{
if (index == numeros.Count - 1)
Console.Write(item + "." + "\n");
else
Console.Write(item + ", ");
index++;
}
index = 0;
Console.WriteLine("\n" + "Números pares:");
foreach (int item in pares)
{
if (index == pares.Count - 1)
Console.Write(item + "." + "\n");
else
Console.Write(item + ", ");
index++;
}
Console.WriteLine("\n" + "Números ímpares:");
index = 0;
foreach (int item in impares)
{
if (index == impares.Count - 1)
Console.Write(item + "." + "\n");
else
Console.Write(item + ", ");
index++;
}
}
}
What would be the requirement ? looks like one of those college exercises, and there’s a teacher who asks for each process to be done in a different loop, rs, would have to see the requirements to know what can be changed
– Rovann Linhalis
That’s right. There’s no rule, just keep the result the way it is. I want to learn a little about optimization to apply in more complex codes in the future.
– Leo_gp