1
2int main(){
3 int arr[5] = {1,3,2,4,5};
4 sort(arr, arr+5, greater<int>());
5 // arr == {5,4,3,2,1}
6 return 0;
7}
8
1#include <iostream>
2using namespace std;
3int main()
4{ // While loop
5 const int SIZE = 9;
6 int arr [SIZE];
7 cout << "Enter numbers: \n";
8 for (int i = 0; i < SIZE; i++)
9 cin >> arr[i];
10 for (int i = 0; i < SIZE; i++)
11 cout << arr[i] << "\t";
12 cout << endl;
13 cout << "Reversed Array:\n";
14 int temp, start = 0, end = SIZE-1;
15 while (start < end)
16 {
17 temp = arr[start];
18 arr[start] = arr[end];
19 arr[end] = temp;
20 start++;
21 end--;
22 }
23 for (int i = 0; i < SIZE; i++)
24 cout << arr[i] << "\t";
25 cout << endl;
26 return 0;
27}
28
1#include <iostream>
2#include <algorithm>
3#include <vector>
4#include <iterator>
5using namespace std;
6int main()
7{
8 vector<int>a = {11,22,33,44,99,55};
9 reverse(a.begin(), a.end());
10 auto it = a.begin();
11 for(it= a.begin(); it!=a.end(); it++){
12 cout << *it << ' ';
13 }
14}
15
1#include <iostream>
2using namespace std;
3int main()
4{
5 const int SIZE = 9;
6 int arr [SIZE];
7 cout << "Enter numbers: \n";
8 for (int i = 0; i < SIZE; i++)
9 cin >> arr[i];
10 for (int i = 0; i < SIZE; i++)
11 cout << arr[i] << "\t";
12 cout << endl;
13 cout << "Reversed Array:\n";
14 int end = SIZE - 1, temp;
15 for (int i = 0; i < end; i++)
16 {
17 temp = arr[i];
18 arr[i] = arr[end];
19 arr[end] = temp;
20 end--;
21 }
22 /* Reverse using while loop
23 int temp, start = 0, end = SIZE-1;
24 while (start < end)
25 {
26 temp = arr[start];
27 arr[start] = arr[end];
28 arr[end] = temp;
29 start++;
30 end--;
31 }*/
32 for (int i = 0; i < SIZE; i++)
33 cout << arr[i] << "\t";
34 cout << endl;
35 }
36
1//java program to reverse array using for loop
2public class ReverseArrayDemo
3{
4 public static void main(String[] args)
5 {
6 int[] arrNumbers = new int[]{2, 4, 6, 8, 10};
7 System.out.println("Given array: ");
8 for(int a = 0; a < arrNumbers.length; a++)
9 {
10 System.out.print(arrNumbers[a] + " ");
11 }
12 System.out.println("Reverse array: ");
13 // looping array in reverse order
14 for(int a = arrNumbers.length - 1; a >= 0; a--)
15 {
16 System.out.print(arrNumbers[a] + " ");
17 }
18 }
19}
1#include<iostream>
2using namespace std;
3int main()
4{
5 int n=3;
6 int arr[]={1,2,3,4};
7 while (n>=0)
8 {
9 cout<<arr[n];
10 n--;
11 }
12
13 return 0;
14}