1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* www.troubleshootyourself.com C program to reverse an array elements. */ #include<stdio.h> int main() { int i, d, n, a[100], b[100]; printf("\nEnter the number of array elements:"); scanf("%d", &n); printf("\nEnter the elements\n", n); for(i = 0; i < n; i++) { scanf("%d", &a[i]); } /* Store the array elements temporarily into array b from array a (read from end of the element)*/ for(i = n-1, d = 0; i >= 0; i--, d++) { b[d] = a[i]; } /* Copy the reversed array into array a. Here we are modifying original array to reverse it. */ for(i = 0; i < n; i++) { a[i] = b[i]; } printf("\n\n Reversed array is: "); for(i = 0; i < n; i++) { printf("%d", a[i]); } return 0; } |
Software engineer by profession, and owned troubleshotyourself channel. Enthusiastic blogger and love to write articles on computer technology and programming. Reach me at kiran.troubleshootyourself@gmail.com.
- Article ends here -