array input in c++ code example

Example 1: print an array c++

#include <iostream>
using namespace std;

int main() {
    int numbers[5] = {7, 5, 6, 12, 35};

    cout << "The numbers are: ";

    //  Printing array elements
    // using range based for loop
    for (const int &n : numbers) {
        cout << n << "  ";
    }


    cout << "\nThe numbers are: ";

    //  Printing array elements
    // using traditional for loop
    for (int i = 0; i < 5; ++i) {
        cout << numbers[i] << "  ";
    }

    return 0;
}

Example 2: array syntax in c++

int foo [] = { 16, 2, 77, 40, 12071 };

Example 3: take input from user in array c++

// declare and initialize an array without defining size
int x[] = {19, 10, 8, 17, 9, 15};