Write a program for reversing of 10 elements if an array in c
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:
- Declare two integer variables, i and j, to iterate over the array.
- Initialize i to 0 and j to 9.
- 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--.
- 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:
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!