Program for Reverse an array | Placement Preparation Question

Write a program to reverse an array or string

Get All TCS CodeVita Questions with Solution

Problem statement:-

Given an array (or string), the task is to reverse the array/string.

Examples : 

Input  : arr[] = {1, 2, 3}

Output : arr[] = {3, 2, 1}


Input :  arr[] = {4, 5, 1, 2}

Output : arr[] = {2, 1, 5, 4}

400+ Coding Questions With Solution[ 110% Placement ]

Source Code of Reverse an Array:


#include<iostream>
using namespace std;
int main(){
    int n,c=0,temp;
    cout<<"enter number of element\n";
    cin>>n;
    int ar[n];
     cout<<"enter "<<n<<" element\n";
    for(int i=0;i<n;i++){
        cin>>ar[i];
    }

    for(int i=n-1;i>=n/2;i--){
        temp=ar[i];
        ar[i]= ar[c];
        ar[c]= temp;
        c++;
    }
    cout<<"After Reverse\n";
    for(int i=0;i<n;i++){
        cout<<ar[i]<<", ";
    }
}


The output of Reverse an Array:



Must Read:-

Final Word:-

In this article, We have given to you the PPT file of the 3D Internet Seminar. We hope you like it

If This post helps you a little bit then please share this post with your friends. For more Computer and programming Related Tips and tricks, please

Follow us on Facebook
Subscribe us on Youtube
Follow us on Instagram
Follow us on Pinterest
follow us on Telegram

Don't forget to share how you like this Content in the comment below. If you have any doubt or suggestion then please write in the comment section below, we will love to hear from you.

                               Thank you.

                                        ( नमस्ते )

                                     🙏 

 

No comments:

Powered by Blogger.