Write a program for adjecents elements reversing of c

06 Sep 2022 Balmiki Mandal 0 C Programming

what are adjacents elements reversing c

"Reversing adjacent elements" in C means reversing the order of every pair of adjacent elements in an array. For example, if the array is {1, 2, 3, 4, 5, 6}, reversing adjacent elements would result in {2, 1, 4, 3, 6, 5}. In other words, the first and second elements are swapped, the third and fourth elements are swapped, and so on.

To reverse adjacent elements in C, you can use a loop that iterates over the array and swaps adjacent elements in each iteration. You would need to ensure that the loop stops before the last element in the array (if the array has an odd number of elements, the last element is not reversed).

Program:  

#include
int main()
{
    int a[10],i,j,t,ele;
    ele=sizeof(a)/sizeof(a[0]);
    printf("Enter the array elements:");
    for(i=0;i<ele;i++)
    scanf("%d",&a[i]);
    printf("Before:");
    for(i=0;i<ele;i++)
    printf(" %d",a[i]);
    printf("\n");
    for(i=0,j=1;i<ele;i=i+2,j=j+2)
    {
        t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
    printf("After:");
    for(i=0;i<ele;i++)
    printf(" %d",a[i]);
    printf("\n");
}

Output:

Enter the array elements: 20 10 40 30 60 50 80 70 100 90

Before: 20 10 40 30 60 50 80 70 100 90

After:    10 20 30 40 50 60 70 80 90 100

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.