-1

so my issue is that i want to print out the info after for loop but it never really reaches the code. Is there a specific reason for that or should i put everything in the scope of the for loop? index outside of bouds of array

`int sumEven=1, sumOdd=1;
            Console.WriteLine("Give the amount of numbers:  ");
            int amount = int.Parse(Console.ReadLine());
            Console.WriteLine("Numbers:  ");
            int[] numberInputs = new int [amount];
            for (int inc = 1; inc <= amount; inc++)
            {
                numberInputs[inc] = int.Parse(Console.ReadLine());
                if (inc % 2 == 0)
                {
                    sumEven *= numberInputs[inc];
                }
                else
                {
                    sumOdd *= numberInputs[inc];
                }
            }
            if (sumEven == sumOdd)
            {
                Console.WriteLine("Yes\n");
                Console.Write($"Product = {sumEven}");
            }
            else if (sumEven != sumOdd)
            {
                Console.Write("No\n");
                Console.WriteLine($"Even product = {sumEven}");
                Console.WriteLine($"Odd product = {sumOdd}");
            }
Luskow
  • 11
  • 1

2 Answers2

0

try to use inc < amount instead of inc <= amount

for (int inc = 0; inc < amount; inc++)
        {
            numberInputs[inc] = int.Parse(Console.ReadLine());
            if (inc % 2 == 0)
            {
                sumEven *= numberInputs[inc];
            }
            else
            {
                sumOdd *= numberInputs[inc];
            }
        }

or

for (int inc = 1; inc < amount; inc++)
        {
            numberInputs[inc] = int.Parse(Console.ReadLine());
            if (inc % 2 == 0)
            {
                sumEven *= numberInputs[inc];
            }
            else
            {
                sumOdd *= numberInputs[inc];
            }
        }

I also recommend you to go through IndexOutOfRangeException

karthik kasubha
  • 392
  • 2
  • 13
-1

You are trying to access an index that does not exits, in most programming languages and also c# arrays start from index 0 so if you have 10 elements your biggest index is 9 therefor when you enter 10 numbers your arrays max index will be 9. Try using < instead of <=

if you just want to ignore the error you can put it in a try catch block and put the code you want to execute in a finally block

You can find an example here https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch-finally