Code Scrappers


Reversing elements of an array C program


              #include<stdio.h>
              #include<stdlib.h>

              void main()
              {
                    int a[20];

                    int i, j, n, temp;

                    printf("\n enter the no of elements : ");

                    scanf("%d", &n);

                    printf("enter the array elements\n");

                    for (i = 0; i < n; i++)
                    {
                        scanf("%d", &a[i]);
                    }

                     j = i - 1;

                     i = 0;

                     while (i < j)
                     {
                          temp = a[i];

                          a[i] = a[j];

                          a[j] = temp;

                          i++;

                          j--;
                     }

                     printf("\n reversed array : ");

                     for (i = 0; i < n; i++)
                     {
                          printf("%d \t", a[i]);
                     }

                  }



          



Output

    enter the no of elements : 5

    enter the array elements
    5
    1
    7
    3
    9

    reversed array : 9     3       7       1       5