





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






#include int main() { int arr[] = {2, 4, 8, 12, 16, 18}; int n = sizeof(arr)/sizeof(arr[0]); // Printing array elements for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Creating an Array
Accessing Array Elements
#include int main() { // array declaration and initialization int arr[5] = {2, 4, 8, 12, 16}; // accessing element at index 2 i.e 3rd element printf("%d ", arr[2]); // accessing element at index 4 i.e last element printf("%d ", arr[4]); // accessing element at index 0 i.e first element printf("%d ", arr[0]); return 0; }
Traversing An Array #include int main() { int arr[5] = {2, 4, 8, 12, 16}; // Print each element of // array using loop printf("Printing Array Elements\n"); for(int i = 0; i < 5; i++){ printf("%d ", arr[i]); } printf("\n"); // Printing array element in reverse printf("Printing Array Elements in Reverse\n"); for(int i = 4; i>=0; i--){ printf("%d ", arr[i]); } return 0; }
Printing Array Elements 2 4 8 12 16 Printing Array Elements in Reverse 16 12 8 4 2 Size of Array
#include int main() { int arr[5] = {2, 4, 8, 12, 16}; // Size of the array int size = sizeof(arr)/sizeof(arr[0]); printf("%d", size); return 0; }