Progress
0%
Arrays  ·  Simple Examples

Arrays — 10 Simple Programs

Very basic programs to practise arrays — from printing and summing to finding max, counting, and reversing. Each program uses only what you already know.

1Print Array
2Sum & Avg
3Max & Min
4Reverse
5Count Even/Odd
6Linear Search
7Copy Array
8Largest Position
9Multiply by 2
10Sort (Bubble)
1

Input and Print an Array

Store & display

The most basic array program. Read n numbers from the user into an array, then print them all. Two loops — one to read, one to print. This pattern is used in almost every array program.

int a[5] after user enters 10 20 30 40 50

a[ ]
10
[0]
20
[1]
30
[2]
40
[3]
50
[4]
print_array.c
C
#include <stdio.h>

int main() {
    int a[10], n, i;

    printf("How many numbers? ");
    scanf("%d", &n);

    /* Read into array */
    for (i = 0; i < n; i++) {
        printf("Enter a[%d]: ", i);
        scanf("%d", &a[i]);
    }

    /* Print array */
    printf("Array: ");
    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}
terminal
output
How many numbers? 5
Enter a[0]: 10
Enter a[1]: 20
Enter a[2]: 30
Enter a[3]: 40
Enter a[4]: 50
Array: 10 20 30 40 50
Key point: scanf("%d", &a[i]) — the & is needed because a[i] is a regular int variable. The index i changes with each loop so each element is filled one by one.
program 2
2

Sum and Average of Array Elements

Accumulator pattern

Add up all elements using a sum variable that starts at 0 and grows with each element. Divide by n to get the average. Use (float)sum / n to get a decimal result.

sum_average.c
C
#include <stdio.h>

int main() {
    int   a[10], n, i, sum = 0;
    float avg;

    printf("How many numbers? ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Enter a[%d]: ", i);
        scanf("%d", &a[i]);
    }

    for (i = 0; i < n; i++)
        sum = sum + a[i];          /* add each element */

    avg = (float)sum / n;          /* cast to get decimal */

    printf("Sum     = %d\n", sum);
    printf("Average = %.2f\n", avg);

    return 0;
}
terminal
output
How many numbers? 4
Enter a[0]: 10
Enter a[1]: 20
Enter a[2]: 30
Enter a[3]: 40
Sum     = 100
Average = 25.00
program 3
3

Find the Largest and Smallest Element

Comparison loop

Start by assuming a[0] is both the max and the min. Then loop through the rest — if any element is bigger than max, update max. If smaller than min, update min.

  • Start from a[0] — never start from 0, use the first actual element
  • Loop from i=1 — a[0] is already set, start comparing from index 1

Finding max and min in {64, 25, 12, 92, 43}

array
64
[0]
25
[1]
12
[2]min
92
[3]max
43
[4]
max_min.c
C
#include <stdio.h>

int main() {
    int a[10], n, i, max, min;

    printf("How many numbers? ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Enter a[%d]: ", i);
        scanf("%d", &a[i]);
    }

    max = a[0];    /* assume first is max */
    min = a[0];    /* assume first is min */

    for (i = 1; i < n; i++) {     /* start from index 1 */
        if (a[i] > max)
            max = a[i];
        if (a[i] < min)
            min = a[i];
    }

    printf("Largest  = %d\n", max);
    printf("Smallest = %d\n", min);

    return 0;
}
terminal
output
How many numbers? 5
64 25 12 92 43
Largest  = 92
Smallest = 12
program 4
4

Print Array in Reverse

Loop backwards

Print the array from the last index n-1 down to 0. No extra array needed — just run the print loop backwards using i--.

Printing {1, 2, 3, 4, 5} in reverse = 5 4 3 2 1

forward
1
[0]
2
[1]
3
[2]
4
[3]
5
[4]
reverse
5
[4]
4
[3]
3
[2]
2
[1]
1
[0]
reverse_array.c
C
#include <stdio.h>

int main() {
    int a[10], n, i;

    printf("How many numbers? ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Enter a[%d]: ", i);
        scanf("%d", &a[i]);
    }

    printf("Reversed: ");
    for (i = n - 1; i >= 0; i--)   /* start from last index */
        printf("%d ", a[i]);

    return 0;
}
terminal
output
How many numbers? 5
1 2 3 4 5
Reversed: 5 4 3 2 1
Key point: Last valid index = n - 1. The loop starts at i = n-1, checks i >= 0, and decrements with i--. It stops after printing index 0.
program 5
5

Count Even and Odd Numbers

Modulus test

Loop through every element and use % 2 to test if it is even or odd. Keep two counters — one for each category. Also print which numbers are even and which are odd.

even_odd.c
C
#include <stdio.h>

int main() {
    int a[10], n, i;
    int even = 0, odd = 0;

    printf("How many numbers? ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Enter a[%d]: ", i);
        scanf("%d", &a[i]);
    }

    printf("Even numbers: ");
    for (i = 0; i < n; i++) {
        if (a[i] % 2 == 0) {
            printf("%d ", a[i]);
            even++;
        }
    }

    printf("\nOdd numbers:  ");
    for (i = 0; i < n; i++) {
        if (a[i] % 2 != 0) {
            printf("%d ", a[i]);
            odd++;
        }
    }

    printf("\nEven count = %d", even);
    printf("\nOdd  count = %d", odd);

    return 0;
}
terminal
output
How many numbers? 6
1 2 3 4 5 6
Even numbers: 2 4 6
Odd numbers:  1 3 5
Even count = 3
Odd  count = 3
program 6
6

Search for a Number in Array

Linear search

Ask the user for a number to find. Loop through the array — if any element matches, print the position and stop. Use a flag variable to remember whether it was found after the loop ends.

linear_search.c
C
#include <stdio.h>

int main() {
    int a[10], n, i, key, found = 0;

    printf("How many numbers? ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        printf("Enter a[%d]: ", i);
        scanf("%d", &a[i]);
    }

    printf("Enter number to search: ");
    scanf("%d", &key);

    for (i = 0; i < n; i++) {
        if (a[i] == key) {
            printf("Found at position %d\n", i);
            found = 1;
            break;           /* stop after first match */
        }
    }

    if (found == 0)
        printf("Number not found\n");

    return 0;
}
terminal
output
Array: 10 20 30 40 50
Search: 30  →  Found at position 2
Search: 99  →  Number not found
program 7
7

Copy One Array Into Another

Two arrays

Create a second array b[] and copy each element from a[] to b[] using a loop. In C you cannot copy arrays by writing b = a — you must copy element by element.

Copying a[] into b[] element by element

a[ ] (source)
5
[0]
10
[1]
15
[2]
20
[3]
b[ ] (copy)
5
[0]
10
[1]
15
[2]
20
[3]
copy_array.c
C
#include <stdio.h>

int main() {
    int a[10], b[10], n, i;

    printf("How many numbers? ");
    scanf("%d", &n);

    printf("Enter elements: ");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    /* Copy a[] into b[] */
    for (i = 0; i < n; i++)
        b[i] = a[i];

    printf("Original array: ");
    for (i = 0; i < n; i++) printf("%d ", a[i]);

    printf("\nCopied  array: ");
    for (i = 0; i < n; i++) printf("%d ", b[i]);

    return 0;
}
terminal
output
How many numbers? 4
5 10 15 20
Original array: 5 10 15 20
Copied  array:  5 10 15 20
Never write b = a to copy arrays. This does not work in C. You must loop through each index and copy one element at a time: b[i] = a[i].
program 8
8

Find Position of Largest Element

Track index, not value

Instead of just finding the maximum value, track its position (index). Start by assuming pos = 0 (index 0 has the max). Update pos whenever a larger element is found.

largest_position.c
C
#include <stdio.h>

int main() {
    int a[10], n, i, pos;

    printf("How many numbers? ");
    scanf("%d", &n);

    printf("Enter elements: ");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    pos = 0;               /* assume index 0 has maximum */

    for (i = 1; i < n; i++) {
        if (a[i] > a[pos])
            pos = i;         /* update position, not value */
    }

    printf("Largest value    = %d\n", a[pos]);
    printf("Found at index   = %d\n", pos);
    printf("Found at position= %d\n", pos + 1); /* human: 1-based */

    return 0;
}
terminal
output
How many numbers? 5
64 25 92 12 43
Largest value     = 92
Found at index    = 2
Found at position = 3
pos vs value: Storing the index pos lets you get both the position (pos) and the value (a[pos]). This is more useful than storing just the value.
program 9
9

Multiply All Elements by 2

Modify array in-place

Loop through the array and change each element directly — a[i] = a[i] * 2. This modifies the array in-place. Print the original first, multiply, then print the new values.

Before and after multiplying each element by 2

before
3
[0]
5
[1]
7
[2]
9
[3]
after ×2
6
[0]
10
[1]
14
[2]
18
[3]
multiply_by_2.c
C
#include <stdio.h>

int main() {
    int a[10], n, i;

    printf("How many numbers? ");
    scanf("%d", &n);

    printf("Enter elements: ");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    printf("Before: ");
    for (i = 0; i < n; i++) printf("%d ", a[i]);

    /* Multiply each element by 2 */
    for (i = 0; i < n; i++)
        a[i] = a[i] * 2;

    printf("\nAfter:  ");
    for (i = 0; i < n; i++) printf("%d ", a[i]);

    return 0;
}
terminal
output
How many numbers? 4
3 5 7 9
Before: 3 5 7 9
After:  6 10 14 18
program 10
10

Sort Array Using Bubble Sort

Nested loops + swap

Compare each adjacent pair. If the left is greater than the right — swap them. Repeat this for n-1 passes. After each pass, the largest unsorted element reaches its correct position at the end.

  • Outer loop — controls the number of passes (n-1 passes needed)
  • Inner loop — compares adjacent elements a[j] and a[j+1]
  • temp — holds one value during the swap so nothing is lost

Bubble sort trace — {64, 25, 12, 92, 43} after each pass

pass 1
25
12
64
43
92
done
pass 2
12
25
43
64
done
92
sorted
12
25
43
64
92
bubble_sort.c
C
#include <stdio.h>

int main() {
    int a[10], n, i, j, temp;

    printf("How many numbers? ");
    scanf("%d", &n);

    printf("Enter elements: ");
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    /* Bubble sort — n-1 passes */
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (a[j] > a[j + 1]) {
                temp      = a[j];
                a[j]      = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }

    printf("Sorted: ");
    for (i = 0; i < n; i++)
        printf("%d ", a[i]);

    return 0;
}
terminal
output
How many numbers? 5
64 25 12 92 43
Sorted: 12 25 43 64 92
The swap needs temp: temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; — always three lines. Without temp, the original value is overwritten and lost before you can copy it.
checklist

Checklist

  • P1 — I can read n numbers into an array and print them
  • P2 — I can find sum and average using an accumulator variable
  • P3 — I can find max and min by starting from a[0], not 0
  • P4 — I can print array backwards using i = n-1 down to 0
  • P5 — I can count even/odd using a[i] % 2 == 0
  • P6 — I can search using a flag variable and break on match
  • P7 — I know arrays must be copied element by element, not b = a
  • P8 — I can track the position (index) of the largest element
  • P9 — I can modify array elements in-place with a[i] = a[i] * 2
  • P10 — I can sort using bubble sort with nested loops and temp swap