Array Of Even Numbers - C# Programming Exercise

In this C# exercise, you are asked to write a program that asks the user for 10 integer numbers and displays the even ones.

This exercise will help you practice the use of loops and conditions in C#. The program should iterate 10 times, asking for a number in each iteration, and then check if the number is even. If it is, the program should display it on the screen.

Exercises like this are very useful to get familiar with numbers and the use of arithmetic and logical operators in C#. Additionally, it provides a great opportunity to improve your skills with data input/output and control flow in your program.

 Category

Arrays, Structures and Strings

 Exercise

Array Of Even Numbers

 Objective

Write a C# program to ask the user for 10 integer numbers and display the even ones.

 Write Your C# Exercise

using System;  // Import the System namespace for basic functionality

class Program  // Define the main class
{
    static void Main()  // The entry point of the program
    {
        // Declare an array to store 10 integers
        int[] numbers = new int[10];
        
        // Ask the user to input 10 integer numbers
        Console.WriteLine("Please enter 10 integer numbers:");

        // Loop to input 10 numbers
        for (int i = 0; i < 10; i++)
        {
            Console.Write($"Enter number {i + 1}: ");
            numbers[i] = Convert.ToInt32(Console.ReadLine());  // Store each number in the array
        }

        // Display the even numbers from the array
        Console.WriteLine("\nThe even numbers are:");
        foreach (int num in numbers)
        {
            if (num % 2 == 0)  // Check if the number is even
            {
                Console.WriteLine(num);  // Print the even number
            }
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Arrays, Structures and Strings

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Array of positive and negative numbers

    In this C# exercise, you are asked to write a program that asks the user for 10 real numbers and displays the average of the positive ones and the average of t...

  •  Many numbers and sum

    In this C# exercise, you are asked to write a program that asks the user for several numbers (until they enter the word "end") and then displays the sum of the...

  •  Two dimensional array

    In this C# exercise, you are asked to write a program that asks the user for marks for 20 pupils (2 groups of 10, using a two-dimensional array), and then disp...

  •  Statistics V2

    In this C# exercise, you are asked to create a statistical program that allows the user to perform the following actions: - Add new data - See all data ...

  •  Struct

    In this C# exercise, you are asked to create a struct to store data of 2D points. The fields for each point will be: - x coordinate (short)

  •  Array of struct

    In this C# exercise, you are asked to expand the previous exercise that used a struct to store 2D points. Now, you need to store up to 1,000 points usin...