C Program to Reverse elements of an array

C Program to Reverse elements of an array

Today, we will learn C Program to Reverse elements of an array .That is:- The last element should be displayed first, followed by second last element and so on.

For Example:-
arr{1,2,3,4,5} is the array input by the user and the reverse order of it should be arr{5,4,3,2,1}

To answer this problem here in C programming language.

C program to reverse array

Algorithm:-

STEP 1: START
STEP 2: INITIALIZE a[100] and b[100]
STEP 3: Enter size of an array.
STEP 4: PRINT “Array Elements:”
STEP 5: REPEAT STEP 6 and STEP 7 UNTIL i=0
STEP 6: PRINT arr[p]
STEP 7: p=p+1
STEP 8: PRINT new line
STEP 9: PRINT “Array in reverse order”
STEP 10: SET p=length-1. REPEAT STEP 11 and STEP 12 UNTIL p>=0
STEP 11: PRINT a[p]
STEP 12: p=p-1
STEP 13: RETURN 0
STEP 14: END

Competitive Coding Techniques

C Program:-

// C code to Reverse of an array
#include 
int main()
    {
        int n, p, q, a[100], b[100];
        printf("Enter the number of elements in an array:\n");      //input size of array
        scanf("%d", &n);

        printf("Enter the elements of an array:\n");       //print elements of an array
        for (p = 0; p < n ; p++)
        scanf("%d", &a[p]);

        for (p = n - 1, p = 0; p >= 0; p--, q++)
        b[q] = a[p];

        for (p = 0; p < n; p++)     //Reverse of digit 
        a[p] = b[p];
        printf("Reverse of an array is:\n");
        
        for (p = 0; p < n; p++)
        printf("%d\n", a[p]);       //print Reverse of an array

        return 0;
    }
Output:Enter the number of elements in an array:
5
Enter the elements of an array:
12
23
30
46
55
Reverse of an array is:
55
46
30
23
12

One comment on “C Program to Reverse elements of an array”


  • Abhishek

    #include
    int main(){
    int a[5]={1,2,3,4,5};
    for (int i = 4; i >= 0; i–)
    {
    printf(“%d “,a[i]);
    }

    return 0;
    }