Reputation: 181
Can anyone shed any light on the subject as to why this is not working, when i am testing it i'm getting lots of random even and odd outputs.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{
int[] array = new int[8] { 1, 2, 3, 4, 8, 5, 7, 9 };
int UserInput = 0;
start:
Console.WriteLine("Please enter a slot: ");
UserInput = Convert.ToInt32(Console.ReadLine());
if(array[UserInput]%2 == 0)
{
Console.WriteLine("Number is even");
}else
{
Console.WriteLine("Number is odd");
}
Console.ReadKey();
goto start;
}
}
}
i want to output if the cell in the array is even or odd not the inputted number so if the number five is inputted it should output even as cell five contains the number eight.
Upvotes: 0
Views: 4869
Reputation: 22158
Arrays are 0-based in C#, so you will need to deduct 1 from the user input. Additionally, you'll want to add some check logic to make sure they're not entering a value higher than 8 or less than 1...
if (UserInput < 1 || UserInput > 8)
{
Console.WriteLine("Please enter a number between 1 and 8");
goto start;
}
else if(array[UserInput - 1] % 2 == 0)
{
Console.WriteLine("Number is even");
}else
{
Console.WriteLine("Number is odd");
}
Upvotes: 7