Reverse Array - C# Programming Exercise

In this C# exercise, you are asked to write a program that prompts the user for 5 numbers, stores them in an array, and displays them in reverse order.

This exercise is great for practicing the use of arrays in C#, a fundamental data structure that allows you to store multiple values in a single variable. Additionally, you will be required to use a loop to display the numbers in reverse order, which involves working with the array indices effectively. The highest index of the array will correspond to the last number entered, and the lowest index to the first.

By completing this exercise, you will have practiced how to iterate over and manipulate arrays in C#, as well as how to manage data flow in a program. It is also a great opportunity to understand how to access array elements using their indices and how the order of elements within a structure can be modified.

 Category

Arrays, Structures and Strings

 Exercise

Reverse Array

 Objective

Write a C# program to ask the user for 5 numbers, store them in an array and show them in reverse order.

 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
    {
        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
        }
    }
}

 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#.

  •  Search in array

    In this C# exercise, you are asked to write a program that checks if a given data belongs to a previously created list. The steps to follow are:

  •  Array of even numbers

    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 w...

  •  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 ...