Print array elements in reverse order

Problem statement:- Program to Print array elements in reverse order using the swapping method.

Data requirement:-

   Input Data:- arr,size

  Output Data:-arr

  Additional Data:- i, j, temp, k

Program in C

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

Code:

#include<stdio.h>
int main()
{
    printf("Enter the size of the array:");
    int size;
    scanf("%d",&size);
    int arr[size],j,i,temp,k,temp2;
    printf("Enter the Element of the array:\n");
     for(j=0;j<size;j++)
        {
        scanf("%d",&arr[j]);
        }
        temp=size;
        while(temp>=0)
        {
            for(k=0;k<temp-1;k++)
            {
                temp2=arr[k];
                arr[k]=arr[k+1];
                arr[k+1]=temp2;
            }
            temp--;
        }
        printf("After reversing array is :\n");
        for(i=0;i<size;i++)
       {
        printf("%d ",arr[i]);
        }
    }

Input/Output:
Enter the size of the array:5
Enter the Element of the array:
20
30
50
40
60
After reversing array is :
60 40 50 30 20

Program in C++

Here is the source code of the C++ Program to reverse an array by swapping the elements and without using any new array.

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,temp,k,temp2;
    cout<<"Enter the Element of the array:\n";
     for(j=0;j<size;j++)
    {
    cin>>arr[j];
    }
    temp=size;
        while(temp>=0)
        {
            for(k=0;k<temp-1;k++)
            {
                temp2=arr[k];
                arr[k]=arr[k+1];
                arr[k+1]=temp2;
            }
            temp--;
        }
        cout<<"After reversing array is :\n";
        for(i=0;i<size;i++)
       {
        cout<<arr[i]<<" ";
        }
    }


Input/Output:
Enter the size of the array:5
Enter the Element of the array:
45
65
75
85
95
After reversing array is :
95 85 75 65 45

Program in Java

Here is the source code of the Java Program to Print array elements in reverse order using the swapping method.

Code:

import java.util.Scanner;
public class p14 {

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=0,i,temp,k,temp2;
System.out.println("Enter the Element of the array:");
     while(j<size)
    {
        arr[j]=sc.nextInt();
        j++;
    }
     temp=size;
        while(temp>=0)
        {
            for(k=0;k<temp-1;k++)
            {
                temp2=arr[k];
                arr[k]=arr[k+1];
                arr[k+1]=temp2;
            }
            temp--;
        }
        System.out.println("After reversing array is :");
        for(i=0;i<size;i++)
       {
        System.out.print(arr[i]+" ");
        }
        sc.close();
}
}

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

Program in Python

Here is the source code of the Python Program to reverse an array by swapping the elements and without using any new array.

Code:

arr=[]
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)
temp=size
while(temp>=0):
    for k in range(0,temp-1,1):
        temp2=arr[k]
        arr[k]=arr[k+1]
        arr[k+1]=temp2
    temp-=1
print("After reversing array is :")
for i in range(0, size):
    print(arr[i],end=" ")

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

Post a Comment

0 Comments