C Program to reverse an array elements



/*
www.troubleshootyourself.com
C program to reverse an array elements. 
*/
 
#include

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;
}
Share this:

Tags:

We will be happy to hear your thoughts

Leave a reply

Troubleshootyourself
Logo