C Program to Reverse Array Elements

C Program to Reverse Array Elements:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().

#include<stdio.h>

int main()
{
   int arr[30],i,j,num,temp;

   printf("Enter no of elements : ");
   scanf("%d",&num);

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

   j=i-1;
   i=0;

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

   //Printing Result
   printf("\nAfter reversing array elements: ");
   for (i=0;i<num;i++)
    {
      printf("%d\t",arr[i]);
    }
}

OUTPUT:

Enter no of elements : 5
Enter 5 Elements: 1 7 3 2 8

After reversing array elements: 8        2       3       7       1

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.