Let Us C (Chapter 8 : Arrays) : Program-6
(f) If an array arr contains n elements, then write a program to check if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on.
OUTPUT
// Let Us C (Chapter 8 : Arrays) : Program-6
/* If an array arr contains n elements, then write a program to check if arr[0] = arr[n-1], arr[1] = arr[n-2] and so on */
#include<stdio.h>
int Arr[100],N,M,i,j=0;
void main()
{
// We basically need to find array is symmetric or not
clrscr();
printf("Program to check if array is symmetric\n");
printf("How many elements?: ");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("Element %d: ",i+1);
scanf("%d",&Arr[i]);
}
M = N/2;
for(i=0;i<M;i++)
{
if(Arr[i] == Arr[N-1-i])
j++;
else
break;
}
if(j==M)
printf("Array is symmetric");
else
printf("Array is asymmetric");
}
Comments
Post a Comment