Saturday, March 28, 2026

Write a program for display values reverse order from an array using a pointer || C Lab Program

 WAP_C05: Write a program for display values reverse order from an array using a pointer.|| Arrays, Pointers and Functions

WAP_C05: C Lab Program


//display values reverse order from an array using a pointer.
#include <stdio.h>

int main() {
    int n, sum = 0, arr[10];

    // Input the number of elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    // Input the array elements
    printf("Enter the elements:\n");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }
   
    int *ptr = arr; 
   
    // Display the array elements in reverse order
    printf("The elements in reverse order are:\n");
    for (int i = n-1; i >= 0; i--) {
      printf("%2d", *(ptr+i));
    }

    return 0;
}



OUTPUT


Enter the number of elements: 5
Enter the elements:
2 4 6 8 10
The elements in reverse order are:
10 8 6 4 2


For all 2026 published articles list: click here

...till the next post, bye-bye & take care.

No comments:

Post a Comment