Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Array Loops Challenge | Arrays
C# Basics

Array Loops ChallengeArray Loops Challenge

Loop through the numbers array and if the value of the element is even, add 1 to it, and if it's odd, multiply it by two. The original array should be modified.

cs

main.cs

1. All the numbers that give a remainder 0 when divided by 2 are called even numbers;
2. Use the loop variable i to index and access the array elements.

using System;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            for(int i = 0; i < numbers.Length; i++)
            {
                if(numbers[i] % 2 == 0)
                {
                    numbers[i] += 1;
                }
                else
                {
                    numbers[i] *= 2;
                }
            }

            foreach(int i in numbers)
            {
                Console.WriteLine(i);
            }
        }
    }
}
      

Все було зрозуміло?

Секція 5. Розділ 8
course content

Зміст курсу

C# Basics

Array Loops ChallengeArray Loops Challenge

Loop through the numbers array and if the value of the element is even, add 1 to it, and if it's odd, multiply it by two. The original array should be modified.

cs

main.cs

1. All the numbers that give a remainder 0 when divided by 2 are called even numbers;
2. Use the loop variable i to index and access the array elements.

using System;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            for(int i = 0; i < numbers.Length; i++)
            {
                if(numbers[i] % 2 == 0)
                {
                    numbers[i] += 1;
                }
                else
                {
                    numbers[i] *= 2;
                }
            }

            foreach(int i in numbers)
            {
                Console.WriteLine(i);
            }
        }
    }
}
      

Все було зрозуміло?

Секція 5. Розділ 8
some-alt