Exercise
Reverse array
Objetive
Write a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order.
Example 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
{
int[] numbers = new int[5]; // Create an array of 5 integers to store the numbers entered by the user
// Ask the user to enter 5 numbers and store them in the array
for (int i = 0; i < 5; i++)
{
Console.Write($"Enter number {i + 1}: "); // Prompt the user for a number
numbers[i] = Convert.ToInt32(Console.ReadLine()); // Convert the input to an integer and store it in the array
}
// Display the numbers in reverse order
Console.WriteLine("\nNumbers in reverse order:");
for (int i = 4; i >= 0; i--)
{
Console.WriteLine(numbers[i]); // Print each number starting from the last element in the array
}
}
}