Computer Science, asked by sweetie9530, 9 months ago

If an array arr contain n elements then write a program to check if arr[0]=arr[n-1] and so on

Answers

Answered by sswaraj04
4

Answer:

#include <iostream>

using namespace std;

int main()

{

   int n;

   cout<<"Enter no. of elements";

   cin>>n;

   int arr[n],count=0;

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

   {

   cout<<"Enter "<<i+1<<"th element";

   cin>>arr[i];

   }

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

   {

       if(arr[i]==arr[n-i-1])

       {

       cout<<"Element at index "<<i+1<<" is equal to that at index "<<n-i<<"\n";

       count++;

       }

   }

   if(count==n/2)

   cout<<"All elements are equal at corresponding index";

   else

   cout<<"all elements are not equal at corresponding index";

   return 0;

}

Explanation:

i have written it in c++

you can use this logic in any language

simply declare an array and feed input to it

now compare value using if(arr[i]==arr[n-i-1])

and printing index of equal elements

running the loop for n/2 times since after n/2 iteration we will be repeating comparisons ex:- for 6 elements 0==6 and 6==0 have same meaning

Similar questions