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.
let us c chapter 8 arrays problem 6
// 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");
}

OUTPUT
let us c chapter 8 arrays problem 6 output

Comments

Popular posts from this blog

Chapter 5 : Functions & Pointers (Let us C)

Let Us C (Chapter 8 : Arrays) : Program-9

Chapter 3: The Loop Control Structure (Let Us C)