'how to print an array backwards

The user enteres a number which is put in an array and then the array needs to be orinted backwadrds

int main()
{
    int numbers[5];
    int x;

    for (int i = 0; i<5; i++)
    {
        cout << "Enter a number: ";
        cin >> x;
        numbers[x];
    }

    for (int i = 5; i>0 ; i--)
    {
        cout << numbers[i];
    }

    return 0;
}


Solution 1:[1]

You are very close to your result but you did little mistakes, the following code is the correct solution of the code you have written.

int main()
{
    int numbers[5];
    int x;

    for (int i = 0; i<5; i++)
    {
        cout << "Enter a number: ";
        cin >> numbers[i];
    }

    for (int i = 4; i>=0; i--)
    {
        cout << numbers[i];
    }

    return 0;
}

Solution 2:[2]

#include<iostream>

using namespace std;
int main()
{
    //get size of the array
    int arr[1000], n;
    cin >> n;
    //receive the elements of the array
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    //swap the elements of indexes
    //the condition is just at "i*2" be cause if we exceed these value we will start to return the elements to its original places 
    for (int  i = 0; i*2< n; i++)
    {
        //variable x as a holder for the value of the index 
        int x = arr[i];
        //index arr[n-1-i]: "-1" as the first index start with 0,"-i" to adjust the suitable index which have the value to be swaped
        arr[i] = arr[n - 1 - i];
        arr[n - 1 - i] = x;
    }
    //loop for printing the new elements
    for(int i=0;i<n;i++)
    {
        cout<<arr[i];
    }
    return 0;
}

Solution 3:[3]

#include <iostream>

using namespace std;

int main() {

//print numbers in an array in reverse order
int myarray[1000];
cout << "enter size: " << endl;
int size;
cin >> size;
cout << "Enter numbers: " << endl;
for (int i = 0; i<size; i++)
{
    cin >> myarray[i];
}

for (int i = size - 1; i >=0; i--)
{
    cout << myarray[i];
}

return 0;

}

of course you can just delete the cout statements and modify to your liking

Solution 4:[4]

this one is more simple

#include<iostream>

using namespace std;

int main ()

{

int a[10], x, i;

cout << "enter the size of array" << endl;

cin >> x;
  cout << "enter the element of array" << endl;

for (i = 0; i < x; i++)
    {

cin >> a[i];

}

cout << "reverse of array" << endl;

for (i = x - 1; i >= 0; i--)

cout << a[i] << endl;

}

Solution 5:[5]

answer in c++. using only one array.

#include<iostream>

using namespace std ;




int main()
{

        int array[1000] , count ;

        cin >> count ;

        for(int i = 0 ; i<count ; i++)
            {

                cin >> array[i] ;
            }

        for(int j = count-1 ; j>=0 ; j--)
            {
                cout << array[j] << endl;
            }
            
    return 0 ;
}

Solution 6:[6]

#include <iostream>
using namespace std;

int main ()
{
    int array[10000];
    int N;

    cout<< " Enter total numbers ";
    cin>>N;

    cout << "Enter  numbers:"<<endl;

    for (int i = 0; i <N; ++i)                                           
    {
        cin>>array[i];
    }
    for ( i = N-1; i>=0;i--)
    {                                                             
        cout<<array[i]<<endl;
    }
    return 0;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 M Umer
Solution 2
Solution 3 David Zhu
Solution 4 Don P
Solution 5
Solution 6 Tunaki