C Code 22 _ To Print the elements in array in given order ( Zoho Coding Round Question)


C Code 22 _ To Print the elements in array in given order ( Zoho Coding Round Question)


Input format :
3           //no of elements
1 2 3     //input three elements to array

Output format:
3 1 2     //print the elements in the order of greatest number in the array as first then smallest element                 next and next greatest elements then the next smallest element and so on... 

Program:



#include<stdio.h>
int main()
{
    int a[50],n,i,t,j;
    printf("\nEnter the number of elements : ");
    scanf("%d",&n);
    printf("\nEnter %d elements : ",n);
    for(i=0;i<n;i++)
    {
            scanf("%d",&a[i]);
    }
    for(i=0;i<n-1;i++)
    {
            for(j=i;j<n;j++)
            {
                            if(i%2==0)
                            {
                                      if(a[i]<a[j])
                                      {
                                                   t=a[j];
                                                   a[j]=a[i];
                                                   a[i]=t;
                                      }
                            }
                            else
                            {
                                      if(a[i]>a[j])
                                      {
                                                   t=a[j];
                                                   a[j]=a[i];
                                                   a[i]=t;
                                      } 
                            }  
            } 
    }
    printf("\n\nThe ordered array elements : ");
    for(i=0;i<n;i++)
    {
                    printf("%d ",a[i]);
    }
    getch();
    return 0;
}



Output:







Thank you ...

Popular Posts