Input and Print an Array
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
#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; }
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
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.Sum and Average of Array Elements
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.
#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; }
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
Find the Largest and Smallest Element
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}
#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; }
How many numbers? 5 64 25 12 92 43 Largest = 92 Smallest = 12
Print Array in Reverse
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
#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; }
How many numbers? 5 1 2 3 4 5 Reversed: 5 4 3 2 1
n - 1. The loop starts at i = n-1, checks i >= 0, and decrements with i--. It stops after printing index 0.Count Even and Odd Numbers
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.
#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; }
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
Search for a Number in Array
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.
#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; }
Array: 10 20 30 40 50 Search: 30 → Found at position 2 Search: 99 → Number not found
Copy One Array Into Another
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
#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; }
How many numbers? 4 5 10 15 20 Original array: 5 10 15 20 Copied array: 5 10 15 20
b[i] = a[i].Find Position of Largest Element
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.
#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; }
How many numbers? 5 64 25 92 12 43 Largest value = 92 Found at index = 2 Found at position = 3
pos lets you get both the position (pos) and the value (a[pos]). This is more useful than storing just the value.Multiply All Elements by 2
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
#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; }
How many numbers? 4 3 5 7 9 Before: 3 5 7 9 After: 6 10 14 18
Sort Array Using Bubble Sort
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
#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; }
How many numbers? 5 64 25 12 92 43 Sorted: 12 25 43 64 92
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
- 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