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. C program to input and print array elements using loop.

Required knowledge

Basic C programming, c-Arrays, C Loops

How to input and print 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 print 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;
}



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

C program to print elements of array using recursion

Write a C program to print all elements of array using recursion. How to display all elements of an array using recursion. Logic to print array elements using recursion in C programming.

Logic to print array elements using recursion

Let us first define our recursive function to print array elements, say printArray(int arr[], int start, int len). The function takes three parameters where first is array to print, second is starting index from where we want to print array and last is length of the array or upper limit to print elements in array. Here

  1. start >= len is used as base condition of recursion. Which will exit function control to the caller function.
  2. If base condition is not satisfied then print arr[start].
  3. After printing array element make recursive call to print successive array element i.e. printArray(arr, start + 1, len);.

Program to print array elements using recursion




 
/**
 * C program to print array elements using recursion.
 */

#include <stdio.h>
#define MAX_SIZE 100

/* Function declaration */
void printArray(int arr[], int start, int len);


int main()
{
    int arr[MAX_SIZE];
    int N, i;
    
    /* Input size and elements in array */
    printf("Enter size of the array: ");
    scanf("%d", &N);
    printf("Enter elements in the array: ");
    for(i=0; i<N; i++) 
    {
        scanf("%d", &arr[i]);
    }
        
    /* Prints array recursively */
    printf("Elements in the array: ");
    printArray(arr, 0, N);
    
    return 0;
}


/**
 * Prints an array recursively within a given range.
 */
void printArray(int arr[], int start, int len)
{
    /* Recursion base condition */
    if(start >= len)
        return;
        
    /* Prints the current array element */
    printf("%d, ", arr[start]);
    
    /* Recursively call printArray to print next element in the array */
    printArray(arr, start + 1, len); 
}



Output

Enter size of the array: 10
Enter elements in the array: 1 2 3 4 5 6 7 8 9 10
Elements in the array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,




Instagram