Exercise
Array Of Even Numbers
Objective
Write a C# program to ask the user for 10 integer numbers and display the even ones.
Example C# Exercise
Show C# Code
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
}
}
}
}
Output
Case 1:
Please enter 10 integer numbers:
Enter number 1: 1
Enter number 2: 2
Enter number 3: 3
Enter number 4: 4
Enter number 5: 5
Enter number 6: 6
Enter number 7: 7
Enter number 8: 8
Enter number 9: 9
Enter number 10: 10
The even numbers are:
2
4
6
8
10
Case 2:
Please enter 10 integer numbers:
Enter number 1: 11
Enter number 2: 12
Enter number 3: 13
Enter number 4: 14
Enter number 5: 15
Enter number 6: 16
Enter number 7: 17
Enter number 8: 18
Enter number 9: 19
Enter number 10: 20
The even numbers are:
12
14
16
18
20
Case 3:
Please enter 10 integer numbers:
Enter number 1: 21
Enter number 2: 22
Enter number 3: 23
Enter number 4: 24
Enter number 5: 25
Enter number 6: 26
Enter number 7: 27
Enter number 8: 28
Enter number 9: 29
Enter number 10: 30
The even numbers are:
22
24
26
28
30
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#.
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...
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...
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...
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 ...
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)
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...