C Code 37 _ To Find the First , Last and Middle Elements in the given array

C Code 37 _ To Find the First , Last and Middle Elements in the given array


Sample Input :

No of element : 3

Elements : 3  6  9

Sample Output:

First Element => 3

Middle Element => 6

Last Element => 9


Note : 

If there are even number of elements in the array we can not find the middle element so in such case print the middle element value as NOT FOUND .


Program:


#include<stdio.h>
int main()
{
int n,i;
int a[100];
printf("Enter no of elements in array : ");
scanf("%d",&n);
if(n>0)
{
printf("\n\nEnter %d elements : ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n\nFirst Element => %d",a[0]);
if(n%2==1)
{
printf("\n\nMiddle Element => %d",a[n/2]);
}
else
{
printf("\n\nMiddle Element => NOT FOUND");
}
printf("\n\nLast Element => %d",a[n-1]);
}
else
{
printf("\n\nEnter only positive values");
}
return 0;
}


Output:








Thank You ...



Popular Posts