Find out if it is even or odd in array

Asked

Viewed 1,699 times

0

I’m trying to define the even and odd numbers of a total of 5 numbers entered by the user in 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)

2 answers

2


You can use the % 2 (MOD) to make this check, if return rest 1, is odd, otherwise is even. I also improved a little its current code:

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 on Ideone.

2

In an optimized and modern code, since you are learning, I find it interesting to know how it really is done, I could make it simpler, but you will not learn common techniques in everyday life.

An error in your algorithm is relying on data entry. If one enters something invalid your program will break. So I used the correct method and checked if what was typed is valid. If it is not, it will prompt for the new number without incrementing the array.

I created the array with size 5 which is what you want. arrays start at 0 and always end at its size minus one, since 0 is an item. I imagine I created with 6 because I was giving error in the last item. Don’t start with 1, this wastes an item and is non-standard, you may even know what to do, but other programmers and will not know and when to interact with arrays of other components there will be an impedance between them and you will have to make adjustments, if you realize that there is a bug in the code, can pass beaten.

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"?.

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")}");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Behold Differences between Parse() vs Tryparse().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.