Write a program for reversing of 10 elements if an array in c

06 Sep 2022 Balmiki Mandal 0 C Programming

Reversing of 10 elements in an array 

To reverse the order of the elements in an array of 10 elements in C, we can use the following steps:

  1. Declare two integer variables, i and j, to iterate over the array.
  2. Initialize i to 0 and j to 9.
  3. While i is less than or equal to j:
    • Swap the values of the elements at indices i and j.
    • Increment i by 1 0r i++.
    • Decrement j by 1 or j--. 
  4. Return the array.

Program :C program to Reversing of 10 elements in an array 

#include<stdio.h>
int main()
{
    int a[10],i,j,t,ele;
    ele=sizeof(a)/sizeof(a[0]);
    printf("Enter Array:");
    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=ele-1;i<j;i++,j--)
    {
        t=a[i];
        a[i]=a[j];
        a[j]=t;
    }
    printf("after:");
    for(i=0;i<ele;i++)
    printf(" %d",a[i]);
    printf("\n");
}

This program first initializes an array of 10 integers with values 1 through 10, and then prints the original array. It then uses a loop to swap the elements of the array, starting with the first and last elements and working inward until the middle of the array is reached. Finally, it prints the reversed array.

output 

Original array: 1 2 3 4 5 6 7 8 9 10 
Reversed array: 10 9 8 7 6 5 4 3 2 1 

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.