C program to reverse an array - IProgramX

Q. Write a program to accept n number and display the array in the reverse order. write separate function  to accept and display.


Program

#include <stdio.h>
int cal(int);
int main()
{
   int n;
   printf("Enter the number of elements in array\n");
   scanf("%d", &n);
   cal(n);
}
int cal(int n)
{
  int c, d, a[100], b[100];
   printf("Enter the array elements\n");
   for (c = 0; c < n ; c++)
      scanf("%d", &a[c]);
   for (c = n - 1, d = 0; c >= 0; c--, d++)
      b[d] = a[c];

   for (c = 0; c < n; c++)
      a[c] = b[c];

   printf("Reverse array is\n");

   for (c = 0; c < n; c++)
      printf("%d\n", a[c]);

   return 0;
}

Output:

Enter the number of elements in array
5
Enter the array elements
3
8
6
5
9
Reverse array is
9
5
6
8
3

Post a Comment

0 Comments