C program to input and print elements in array

Write a C program to input elements in array and print array. How to input and display elements in an array using for loop in C programming.

Example

Input

Input size: 10
Input elements: 1
2
3
4
5
6
7
8
9
10

Output

Output: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Required knowledge

Basic C programming, For loop, Array

How to input and display array elements

Array uses an index for accessing an element. Array index starts from 0 to N-1 (where N is the number of elements in array).

Array and array index representation

To access any an array element we use.

array[0] = 10
array[1] = 20
array[2] = 30

array[9] = 100

Since array index is an integer value. Hence, rather hard-coding constant array index, you can use integer variable to represent index. For example,

int i = 0;
array[i] = 10; // Assigns 10 to first array element

Program to input and display array elements

/**
 * C program to read and print elements in an array
 */

#include <stdio.h>
#define MAX_SIZE 1000 // Maximum array size

int main()
{
    int arr[MAX_SIZE]; // Declare an array of MAX_SIZE
    int i, N;

    /* Input array size */
    printf("Enter size of array: ");
    scanf("%d", &N);

    /* Input elements in array */
    printf("Enter %d elements in the array : ", N);
    for(i=0; i<N; i++)
    {
        scanf("%d", &arr[i]);
    }

    /*
     * Print all elements of array
     */
    printf("\nElements in array are: ");
    for(i=0; i<N; i++)
    {
        printf("%d, ", arr[i]);
    }

    return 0;
}

Note: Using i < N is equivalent to i <= N-1.

Advance your skills by learning this using recursive approach.

Learn more - Program to print array elements using recursion.

Output

Enter size of array: 10
Enter 10 elements in the array : 10
20
30
40
50
60
70
80
90
100

Elements in array are : 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>