C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Input and Display Array Elements

C Program: Input and Display Array Elements

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Display array elements

    printf("\nArray elements are:\n");

    for(i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
10 20 30 40 50

OUTPUT :
Array elements are:
10 20 30 40 50

Explanation

  1. The program first asks the user how many elements (n) they want to store.
  2. Using a for loop, it takes n integer inputs from the user and stores them in the array arr.
  3. Another for loop displays all the entered elements one by one.
  1. Arrays in C are used to store multiple elements of the same data type in a single variable.
  2. Accessing and iterating through array elements is typically done using loops (for, while, etc.).

 

C Program: Input and Display Array Elements

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements using while loop

    printf("Enter %d elements:\n", n);

    while(i < n) {

        scanf("%d", &arr[i]);

        i++;

    }

 

    // Display array elements using while loop

    i = 0;

    printf("\nArray elements are:\n");

    while(i < n) {

        printf("%d ", arr[i]);

        i++;

    }

 

    return 0;

}

Output

 
INPUT  :
Enter number of elements: 4
Enter 4 elements:
5 10 15 20

OUTPUT :
Array elements are:
5 10 15 20

Explanation

  1. The user enters the number of elements (n).
  2. The while loop runs until all elements are entered and stored in the array.
  3. Another while loop displays the array elements one by one.
  4. The while loop is used when the number of iterations is known but initialization and increment need to be handled manually.
  5. This program demonstrates array input/output without using for

 

C Program: Input and Display Array Elements

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements using do...while loop

    printf("Enter %d elements:\n", n);

    do {

        scanf("%d", &arr[i]);

        i++;

    } while(i < n);

 

    // Display array elements using do...while loop

    i = 0;

    printf("\nArray elements are:\n");

    do {

        printf("%d ", arr[i]);

        i++;

    } while(i < n);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
2 4 6 8 10

OUTPUT :
Array elements are:
2 4 6 8 10

Explanation

  1. The user is first prompted to enter how many elements (n) they want in the array.
  2. The do..while loop ensures that the block runs at least once, taking input from the user.
  3. The same looping structure is used again to display the entered elements.
  4. The do..while loop always executes the loop body once before checking the condition.
  5. This makes it useful for cases like taking user input, where at least one iteration is always required.