Write a ‘C’ program to Read an array of 20 elements and then send all negative elements of the array to the end without altering the original sequence.


#include<stdio.h>
void del_pos(int a[],int n,int p)
{
        int i;
        for(i=p;i<n-1;i++)
        {
                a[i]=a[i+1];
        }
}
main()
{
        int a[100],i,j,n,ele,temp;
        printf("\nEnter how many terms=");
        scanf("%d",&n);
        printf("\nEnter the elements=");
        for(i=0;i<n;i++)
        {
                scanf("%d",&a[i]);
        }
        printf("\nThe elements are=");
        for(i=0;i<n && a[i]>=0;i++)
        {
                printf("%d ",a[i]);
        }
        printf("\nAfter sending negative elements to the end");
        i=0;
        while(a[i]<0)
        {
                if(a[i]<0)
                {
                        temp=a[i];
                        del_pos(a,n,i);
                        a[n-1]=temp;
                }
                if(a[i]<0)
                        continue;
                i++;
        }
        printf("\n");
        for(j=0;j<n;j++)
        {
                printf("%d ",a[j]);
        }
}

No comments:

Post a Comment