This article covers some common array programs in C with code, output, and explanation.
Table of Contents
Program to Find Sum of Array Elements
This program adds all elements present in an array.
// C program to find the sum of
// array elements
#include stdio.h
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int sum = 0;
int i;
for(i = 0; i 5; i++)
{
sum = sum + arr[i];
}
printf("Sum = %d", sum);
return 0;
}
Output:
Explanation:Sum = 150
The loop visits each element of the array and keeps adding it to the sum variable.
Program to Find Largest Element in an Array
This program finds the highest value stored in the array.
// C program to find largest
// element in an array
#include stdio.h
int main()
{
int arr[5] = {25, 40, 15, 90, 60};
int max = arr[0];
int i;
for(i = 1; i 5; i++)
{
if(arr[i] max) {
max = arr[i];
}
}
printf("Largest Element = %d", max);
return 0;
}
Output:
Explanation:Largest Element = 90
The first element is assumed as the largest value. The loop compares every element and updates the maximum value.
Program to Find Smallest Element in an Array
This program identifies the minimum value in the array.
// C program to find smallest
// element in an array
#include stdio.h
int main()
{
int arr[5] = {25, 40, 15, 90, 60};
int min = arr[0];
int i;
for(i = 1; i 5; i++)
{
if(arr[i] min) {
min = arr[i];
}
}
printf("Smallest Element = %d", min);
return 0;
}
Output:
Explanation:Smallest Element = 15
The program checks every element and stores the smallest value found.
Program to Reverse an Array
This program prints array elements in reverse order.
// C program to reverse an array
#include stdio.h
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
int i;
printf("Reversed Array: ");
for(i = 4; i = 0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:
Explanation:Reversed Array: 5 4 3 2 1
The loop starts from the last index and prints elements until the first index.
Program to Count Even and Odd Numbers
This program counts even and odd elements separately.
// C program to count even
// and odd numbers
#include stdio.h
int main()
{
int arr[6] = {12, 7, 18, 5, 20, 9};
int even = 0, odd = 0;
int i;
for(i = 0; i 6; i++)
{
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Even Numbers = %d\n", even);
printf("Odd Numbers = %d", odd);
return 0;
}
Output:
Explanation:Even Numbers = 3
Odd Numbers = 3
If a number is divisible by 2, it is counted as even. Otherwise, it is counted as odd.
Program to Sort Array Elements in Ascending Order
This program arranges elements from smaller to larger values.
// C program to sort array elements
// in ascending order
#include stdio.h
int main()
{
int arr[5] = {50, 10, 30, 20, 40};
int i, j, temp;
for(i = 0; i 5; i++)
{
for(j = i + 1; j 5; j++)
{
if(arr[i] arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Sorted Array: ");
for(i = 0; i 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Output:
Explanation:Sorted Array: 10 20 30 40 50
The program compares elements and swaps them whenever a smaller value is found.
Key Points About Array Programs
- Element Access Using Index: Array elements are accessed using index numbers starting from 0.
- Fixed Size Storage: The size of an array is decided during declaration and cannot change later.
- Same Data Type Storage: An array stores only one type of data such as integers, characters, or floats.
- Useful for Loop Operations: Arrays work efficiently with loops for searching, sorting, and calculations.
- Important for Interview Preparation: Many placement and coding interview questions are based on array concepts.
Conclusion
Array programs are commonly asked in C programming assignments, exams, and interviews. Learning basic operations such as finding sum, maximum value, sorting, and reversing helps build strong programming logic. Once these programs become easy, you can move to searching algorithms, matrix operations, and advanced array problems.Frequently Asked Questions
1. What is an array in C?2. Why are arrays used in C?An array is a collection of elements of the same data type stored in continuous memory locations.
3. Can arrays store different data types together?Arrays help store and manage multiple values using a single variable name.
4. Which loop is mostly used with arrays?No, arrays can store only one data type at a time.
5. Are array programs important for interviews?The for loop is most commonly used for array traversal.
Yes, array based questions are frequently asked in coding tests and technical interviews.
0 Comments