WAP_C04 Write a program for reading elements using a pointer into an array and display the values using the array.|| Arrays, Pointers and Functions
WAP_C04: C Lab Program
//reading elements using a pointer into an array and display the values using the array. #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);
int *ptr = arr; printf("Enter the elements:\n"); for (int i = 0; i < n; i++) { // Read element using the pointer scanf("%d", ptr + i); // Pointer arithmetic to read the value }
// Display the values using the array printf("The elements are:\n"); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); // Display elements using the array }
return 0; } |
OUTPUT
Enter the number of elements: 5 Enter the elements: 2 4 6 8 10 The elements are: 2 4 6 8 10 |
For all 2026 published articles list: click here
...till the next post, bye-bye & take care.
No comments:
Post a Comment