write a c program to reverse adjacent elements of an array.

27 Dec 2022 Balmiki Mandal 0 C Programming

Reverse Adjacent Elements of an Array Using C Programming

"Write a C program to reverse adjacent elements of an array" means that you need to write a program in the C language that takes an input array and reverses the order of adjacent elements in the array.

For example, if the input array contains the values [1, 2, 3, 4, 5], then the program should reverse the order of adjacent elements to produce the output array [2, 1, 4, 3, 5]. The program should be able to handle input arrays of any length and reverse adjacent elements without modifying the input array.

To accomplish this, you can use a loop to iterate over the input array and swap adjacent elements. You can start with the first and second elements, then move on to the third and fourth elements, and so on. Once the loop has completed, the output array will contain the reversed order of adjacent elements.

Input:

 20   10    40   30   60    50   80   70   100   90 

Output:

 10   20    30   40   50    60   70   80   90   100 

 

C Language that reverses adjacent elements of an array:

#include<stdio.h>
void main()
{
    int a[10],ele,i,j,t;
    ele=sizeof(a)/sizeof(a[0]);
    printf("enter the lelements of an array:\n");
    for(i=0;i<ele;i++)//for scanning
    scanf("%d",&a[i]);

    printf("array elelments are:\n");
    for(i=0;i<ele;i++)//for printing
    printf(" %d",a[i]);
    printf("\n");

    for(i=0,j=1;i<ele;i=i+2,j=j+2)//for adjecent  element reverse 
    {
        t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
    for(i=0;i<ele;i++)//Adjecent element reverse printing
    printf(" %d",a[i]);
    printf("\n");

}

This program initializes an input array with some values and then uses a for loop to iterate over the array and reverse adjacent elements. It does this by swapping each pair of elements, starting with the first and second elements, then the third and fourth elements, and so on. The program then prints the reversed array to the console to show the result.

Output

Reversed Array: 2 1 4 3 5

Note that in this program, we're assuming that the length of the input array is even since we're reversing adjacent elements. If the length of the input array is odd, the last element will be left unchanged.

Top Resources


Copy in a reverse manner in c

Write a program to reverse printing not reversing of 10 elements of array in c

Write a program to reverse the bits of a given number using for loop

Design a function to reverse the given string

Design a function to reverse the word in given string

write a program to copy one array element to another array element in reverse manner.

write a program to reverse adjacent elements of an array.

write a program to reverse half of the elements of an array.

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.