Find out if it is even or odd in array

0

I'm trying to set the even and odd numbers of a total of 5 numbers entered by the user into a vector.

        int[] atividade = new int[6];
        for (int i = 1; i < 6; i++)
        {
            Console.WriteLine("Insira o " + i + "° numero: ");
            atividade[i] = Int32.Parse(Console.ReadLine());             
        }            
        Console.WriteLine("Os seguintes numeros foram digitados: ");

        foreach (int i in atividade)
    
asked by anonymous 20.08.2017 / 04:37

2 answers

2

You can use % 2 (MOD) to do this check, if it returns rest 1, it is odd, otherwise it is even. I've also improved your current code a bit:

using System;
using static System.Console;

public class Test
{
    public static void Main()
    {
        int[] atividade = new int[6];
        string numeros = "", tipo = "";
        for (int i = 1; i < 6; i++)
        {
            WriteLine($"Insira o {i}° numero: ");
            atividade[i] = int.Parse(Console.ReadLine());
            tipo += atividade[i] % 2 == 1 ? "impar, " : "par, ";
            numeros += atividade[i] + ", ";
        }
        Clear();
        Write($"Os seguintes numeros foram digitados: \n{numeros}\n{tipo}");
    }
}

See working at Ideone .

    
20.08.2017 / 05:33
2

I am an optimized and modern code since it is learning. I think it is interesting to know how it really is done, I could do it simpler, but you will not learn common techniques everyday.

An error in your algorithm is relying on data entry. If the person types something invalid your program will break. So I used the correct method and checked that what was entered is valid. If it is not it will ask for the new number without incrementing the array control variable.

I created the array with size 5 that was what you wanted. arrays start at 0 and always end at their size minus one, since 0 is an item. I imagine you created it with 6 because you were giving the last item an error. Do not start with 1, this wastes an item and is out of standard, you might even know what to do, but other programmers will not know and when it will interact with arrays of other components there will be an impedance between them you will have to make adjustments, if you notice that there is a bug in the code, you can get hit.

I also used a constant, so if you want to change the number of items you only need to change in one place. See About What are "Magic Numbers" in JavaScript? .

using static System.Console;

public class Test {
    public static void Main() {
        const int tamanho = 5;
        var atividade = new int[tamanho];
        var i = 0;
        while (i < tamanho) {
            WriteLine($"Insira o {i}° numero: ");
            if (int.TryParse(ReadLine(), out atividade[i])) {
                i++;
            }
        }
        WriteLine("Os seguintes numeros foram digitados: ");
        foreach (var item in atividade) {
            WriteLine($"{item} é {((item & 1) == 0 ? "par" : "impar")}");
        }
    }
}

See running on .NET Fiddle . And at Coding Ground . Also I put GitHub for future reference .

See Differences between Parse vs TryParse .

    
20.08.2017 / 14:47