Write a C program for reading elements using a pointer into an array and display the values using the array.
Write a C program for reading elements using a pointer into an array and display the values using the array.
Algorithm
start
step1:
step2:
step3:
step4:
step5:
step6:
step7:
step8:
step9:
step10:
stop
Flowchart
C Programming
//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
๐ป Run This C Program
Write a C program for reading elements using a pointer into an array and display the values using the array.
The selected C program is loaded automatically. Edit the code,
provide input through STDIN and click Run.
The compiler is embedded using OneCompiler. Your program runs in
the embedded C environment; Study Glance does not execute C code
directly on your web server.