Question #47756

c++ program to print array in reverse order.

Expert's answer

#include <iostream>

using namespace std;

int main()

{

int a[5]; // init array

int i; // counter

int n = 5; // count values in array

cout << "Enter 5 number: ";

// dispaly start values

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

cin >> a[i];

}

// reverse array

for ( int i = 0; i < n / 2; i++ ) {

int tmp = a[i];

a[i] = a[ n - i - 1 ];

a[ n - i - 1 ] = tmp;

}

cout << endl; // new string

// dispaly finish values

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

cout << a[i]

<< " ";

}

return 0;

}

using namespace std;

int main()

{

int a[5]; // init array

int i; // counter

int n = 5; // count values in array

cout << "Enter 5 number: ";

// dispaly start values

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

cin >> a[i];

}

// reverse array

for ( int i = 0; i < n / 2; i++ ) {

int tmp = a[i];

a[i] = a[ n - i - 1 ];

a[ n - i - 1 ] = tmp;

}

cout << endl; // new string

// dispaly finish values

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

cout << a[i]

<< " ";

}

return 0;

}

``

## Comments

## Leave a comment