Print array elements in reverse order

Problem statement:- Program to Print array elements in reverse order.

Data requirement:-

   Input Data:- arr,size

  Output Data:-arr

  Additional Data:- i, j

Program in C

Here is the source code of the C Program to Print array elements in reverse order.

Code:

#include<stdio.h>
int main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size],j=0,i;

    printf("Enter the Element of the array:\n");
     for(j=1;j<=size;j++)
        {
        scanf("%d",&arr[j]);
        }
        printf("After reversing array is :\n");
        for(i=size;i>=1;i--)
       {
        printf("%d ",arr[i]);
        }
    }

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
1
2
3
4
5
After reversing array is :
5 4 3 2 1

Program in C++

Here is the source code of the C++ Program to Print array elements in reverse order.

Code:

#include<iostream>
using namespace std;
int main()
{
    cout<<"Enter the size of the array:";
    int size;
    cin>>size;
    int arr[size],j=0,i;
    cout<<"Enter the Element of the array:\n";
     for(j=1;j<=size;j++)
    {
    cin>>arr[j];
    }
    cout<<"After reversing array is :\n";
        for(i=size;i>=1;i--)
       {
        cout<<arr[i]<<" ";
        }
    }

Input/Output:
Enter the size of the array:4
Enter the Element of the array:
10
12
17
16
After reversing array is :
16 17 12 10

Program in Java

Here is the source code of the C Program to Print array elements in reverse order.

Code:

import java.util.Scanner;
public class p13 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
    int size;
    size=sc.nextInt();
    int arr[ ]=new int[size];
int j;
System.out.println("Enter the Element of the array:");
       for(j=0;j<size;j++)
        {
        arr[j]=sc.nextInt(); 
        }
       System.out.println("After reversing array is :");
        for(int i=size-1;i>=0;i--)
       {
        System.out.print(arr[i]+" ");
        }
     sc.close();
}
}

Input/Output:
Enter the size of the array:
5
Enter the Element of the array:
9
12
32
6
7
After reversing array is :
7 6 32 12 9 

Program in Python

Here is the source code of the Python Program to Print array elements in reverse order.

Code:

arr=[]
cout=0
sum=0
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)
print("After reversing array is :");
for i in range(size-1,-1,-1):
    print(arr[i],end=" ")

Input/Output:
Enter the size of the array: 3
Enter the Element of the array:
7
12
3
After reversing array is :
3 12 7

Post a Comment

0 Comments