C Program To Reverse An Array

C Program To Reverse An Array. If you are looking for C Program for Reverse Array, here in this tutorial we will help you to learn how to write a C program to reverse an array elements.

C Program To Reverse An Array

Learn how to write a C program to reverse an array. Writing a C reverse array program can be done using various techniques but here in this program, we show how to write a program to reverse an array logic in a proper way.

Reversing An Array Elements Example

These are some examples of reversing array elements.

Input : arr[] = {11, 12, 13}
Output : arr[] = {13, 12, 11}

Input : arr[] = {41, 51, 11, 21}
Output : arr[] = {21, 11, 51, 41}

C Program To Reverse An Array Source Code

Find the below source code to reverse an array and how to write c program to reverse an array using while loop.

/* C Proram to reverse an array using while loop - ReverseArray.C */

#include<stdio.h>

int main() {
//variable declaration
 int arr[30], i, j, num, temp;
//asking user to enter size of an array elements
printf("\nEnter no of elements : ");
 scanf("%d", &num);

//Reading elements in an array
 for (i = 0; i < num; i++) {
 scanf("%d", &arr[i]);
 }

j = i - 1; // j will Point to last element of an array
 i = 0; // i will be pointing to first element of ann array

while (i < j) {
 temp = arr[i];
 arr[i] = arr[j];
 arr[j] = temp;
 i++; // increment i
 j--; // decrement j
 }

//Printing output of an array elements in a reverse order
 printf("\nThe result after reversing of an array : ");
 for (i = 0; i < num; i++) {
 printf("%d \t", arr[i]);
 }

return (0);
}

C Program To Reverse An Array Output

Enter no of elements : 3

41     51     11     21

The result after reversing of an array

21     11     51     41

C PROGRAMMING TUTORIALS

  1. C Program For Pascal Triangle
  2. C Program to Print Prime Numbers from 1 to 100
  3. C Program to Find LCM and GCD of Two Numbers
  4. C Program to Add Spaces in a String
  5. C Program To Add Digits Of A Number
  6. C Program To Find Leap Year
  7. C Program To Append Data Into A File
  8. C Program to Accept Only Integer Values
  9. C Program To Arrange Numbers In Descending Order
  10. C Program to Add Days to Date
  11. C Program to Add Two Fractions
  12. C Program To Reverse A Number
  13. C Program to Find Maximum and Minimum Numbers
  14. C Program to Read a Text File
  15. C Program to Convert Decimal to Hexadecimal
  16. C Program to Convert Decimal to Binary
  17. C Program to Convert Celsius to Fahrenheit
  18. C Program To Find Absolute Value
  19. Ternary Operator Program in C
  20. C Program For Addition Table Using For Loop

Leave a Comment