Arrays are one of the most important data structures in C programming. An array allows you to store multiple values of the same data type in a single variable. Instead of creating separate variables for similar data, arrays help organize and manage data efficiently.
Arrays are widely used in real-world applications such as student management systems, payroll software, inventory systems, searching algorithms, sorting algorithms, and data analysis. C Arrays practice questions with solutions help to understand the concepts.
In this chapter, you’ll practice beginner-friendly array programs with complete solutions, sample outputs, explanations, and concepts covered.
1. C Program to Store and Print Elements of an Array
Problem Statement
Write a C program to store 5 integers in an array and print all the elements.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
printf("\nArray Elements:\n");
for(i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Enter 5 numbers:
10
20
30
40
50
Array Elements:
10 20 30 40 50
Explanation
The first for loop stores the values entered by the user into the array.
The second for loop accesses each element using its index and displays it.
Concepts Covered
- Arrays
- Array Indexing
- for Loop
- User Input
2. C Program to Find the Sum of Array Elements
Problem Statement
Write a C program to calculate the sum of all elements in an array.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, sum = 0;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
sum = sum + numbers[i];
}
printf("Sum = %d", sum);
return 0;
}
Sample Output
Enter 5 numbers:
10
20
30
40
50
Sum = 150
Explanation
The program stores the array elements and simultaneously adds each element to the variable sum.
Concepts Covered
- Arrays
- Accumulator Variable
- Loop
- Arithmetic Operations
3. C Program to Find the Largest Element in an Array
Problem Statement
Write a C program to find the largest element in an array.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, largest;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
largest = numbers[0];
for(i = 1; i < 5; i++)
{
if(numbers[i] > largest)
{
largest = numbers[i];
}
}
printf("Largest Element = %d", largest);
return 0;
}
Sample Output
Enter 5 numbers:
45
12
89
56
34
Largest Element = 89
Explanation
The program assumes the first element is the largest and compares it with every remaining element.
If a larger element is found, the value of largest is updated.
Concepts Covered
- Arrays
- Searching
- if Statement
- Loop
4. C Program to Find the Smallest Element in an Array
Problem Statement
Write a C program to find the smallest element in an array.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, smallest;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
smallest = numbers[0];
for(i = 1; i < 5; i++)
{
if(numbers[i] < smallest)
{
smallest = numbers[i];
}
}
printf("Smallest Element = %d", smallest);
return 0;
}
Sample Output
Enter 5 numbers:
45
12
89
56
34
Smallest Element = 12
Explanation
The program assumes the first array element is the smallest. It then compares it with the remaining elements and updates the value whenever a smaller element is found.
Concepts Covered
- Arrays
- Searching
- if Statement
- Comparison Logic
5. C Program to Calculate the Average of Array Elements
Problem Statement
Write a C program to calculate the average of all elements stored in an array.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, sum = 0;
float average;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
sum = sum + numbers[i];
}
average = (float)sum / 5;
printf("Average = %.2f", average);
return 0;
}
Sample Output
Enter 5 numbers:
10
20
30
40
50
Average = 30.00
Explanation
The program first calculates the sum of all array elements. It then divides the sum by the total number of elements to find the average.
Formula:
Average = Sum of Elements / Total Number of Elements
Concepts Covered
- Arrays
- Sum Calculation
- Average Formula
- Float Type Casting
6. C Program to Count Even and Odd Numbers in an Array
Problem Statement
Write a C program to count the total number of even and odd elements in an array.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i;
int even = 0, odd = 0;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
if(numbers[i] % 2 == 0)
even++;
else
odd++;
}
printf("Even Numbers = %d\n", even);
printf("Odd Numbers = %d", odd);
return 0;
}
Sample Output
Enter 5 numbers:
10
15
8
25
30
Even Numbers = 3
Odd Numbers = 2
Explanation
The program checks every array element.
- If it is divisible by 2, the even counter increases.
- Otherwise, the odd counter increases.
Concepts Covered
- Arrays
- Modulus Operator
- Loop
- Conditional Statements
7. C Program to Reverse an Array
Problem Statement
Write a C program to print the elements of an array in reverse order.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
printf("Reversed Array:\n");
for(i = 4; i >= 0; i--)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Enter 5 numbers:
10
20
30
40
50
Reversed Array:
50 40 30 20 10
Explanation
Instead of starting from index 0, the loop starts from the last index and prints elements in reverse order.
Concepts Covered
- Arrays
- Reverse Traversal
- Loop Control
8. C Program to Copy One Array into Another
Problem Statement
Write a C program to copy all elements from one array into another array.
C Solution
#include <stdio.h>
int main()
{
int source[5], destination[5];
int i;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &source[i]);
}
for(i = 0; i < 5; i++)
{
destination[i] = source[i];
}
printf("Copied Array:\n");
for(i = 0; i < 5; i++)
{
printf("%d ", destination[i]);
}
return 0;
}
Sample Output
Enter 5 numbers:
5
10
15
20
25
Copied Array:
5 10 15 20 25
Explanation
Each element from the source array is copied into the destination array using a loop.
Concepts Covered
- Arrays
- Copying Data
- for Loop
9. C Program to Search an Element in an Array
Problem Statement
Write a C program to search for a given element in an array.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, search;
int found = 0;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
printf("Enter element to search: ");
scanf("%d", &search);
for(i = 0; i < 5; i++)
{
if(numbers[i] == search)
{
found = 1;
break;
}
}
if(found)
printf("Element Found");
else
printf("Element Not Found");
return 0;
}
Sample Output
Enter 5 numbers:
12
25
38
41
56
Enter element to search: 38
Element Found
Explanation
The program checks every element until the required value is found.
If found, the loop stops using the break statement.
Concepts Covered
- Arrays
- Linear Search
- break Statement
- Conditional Statements
10. C Program to Sort an Array in Ascending Order
Problem Statement
Write a C program to sort an array in ascending order.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, j, temp;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
for(i = 0; i < 5; i++)
{
for(j = i + 1; j < 5; j++)
{
if(numbers[i] > numbers[j])
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
printf("Sorted Array:\n");
for(i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Enter 5 numbers:
45
12
67
8
34
Sorted Array:
8 12 34 45 67
Explanation
The program compares every element with the remaining elements and swaps them whenever a smaller value is found.
This arranges the array in ascending order.
Concepts Covered
- Arrays
- Nested Loops
- Sorting
- Swapping Elements
11. C Program to Sort an Array in Descending Order
Problem Statement
Write a C program to sort the elements of an array in descending order.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, j, temp;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
for(i = 0; i < 5; i++)
{
for(j = i + 1; j < 5; j++)
{
if(numbers[i] < numbers[j])
{
temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
printf("Sorted Array (Descending):\n");
for(i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Enter 5 numbers:
12
56
18
4
35
Sorted Array (Descending):
56 35 18 12 4
Explanation
The program compares each array element with the remaining elements. If a larger element is found, the values are swapped, resulting in a descending order.
Concepts Covered
- Arrays
- Nested Loops
- Descending Sorting
- Swapping Elements
12. C Program to Find the Second Largest Element in an Array
Problem Statement
Write a C program to find the second largest element in an array.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i;
int largest, secondLargest;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
largest = secondLargest = numbers[0];
for(i = 1; i < 5; i++)
{
if(numbers[i] > largest)
{
secondLargest = largest;
largest = numbers[i];
}
else if(numbers[i] > secondLargest && numbers[i] != largest)
{
secondLargest = numbers[i];
}
}
printf("Second Largest Element = %d", secondLargest);
return 0;
}
Sample Output
Enter 5 numbers:
15
42
98
65
27
Second Largest Element = 65
Explanation
The program maintains two variables:
largestsecondLargest
These variables are updated while traversing the array only once.
Concepts Covered
- Arrays
- Searching
- Comparison Logic
- Single Traversal
13. C Program to Merge Two Arrays
Problem Statement
Write a C program to merge two arrays into a single array.
C Solution
#include <stdio.h>
int main()
{
int first[3], second[3], merged[6];
int i;
printf("Enter 3 elements for first array:\n");
for(i = 0; i < 3; i++)
{
scanf("%d", &first[i]);
}
printf("Enter 3 elements for second array:\n");
for(i = 0; i < 3; i++)
{
scanf("%d", &second[i]);
}
for(i = 0; i < 3; i++)
{
merged[i] = first[i];
merged[i + 3] = second[i];
}
printf("Merged Array:\n");
for(i = 0; i < 6; i++)
{
printf("%d ", merged[i]);
}
return 0;
}
Sample Output
Enter 3 elements for first array:
10
20
30
Enter 3 elements for second array:
40
50
60
Merged Array:
10 20 30 40 50 60
Explanation
The program copies all elements of the first array, followed by the elements of the second array, into a new merged array.
Concepts Covered
- Arrays
- Array Copying
- Merging Arrays
14. C Program to Find Duplicate Elements in an Array
Problem Statement
Write a C program to find duplicate elements in an array.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, j;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
printf("Duplicate Elements:\n");
for(i = 0; i < 5; i++)
{
for(j = i + 1; j < 5; j++)
{
if(numbers[i] == numbers[j])
{
printf("%d ", numbers[i]);
}
}
}
return 0;
}
Sample Output
Enter 5 numbers:
10
20
30
20
10
Duplicate Elements:
10 20
Explanation
Each array element is compared with the remaining elements to identify duplicate values.
Concepts Covered
- Arrays
- Nested Loops
- Duplicate Detection
- Comparison Logic
15. C Program to Delete an Element from an Array
Problem Statement
Write a C program to delete an element from an array at a specified position.
C Solution
#include <stdio.h>
int main()
{
int numbers[5];
int i, position;
printf("Enter 5 numbers:\n");
for(i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
printf("Enter position to delete (1-5): ");
scanf("%d", &position);
for(i = position - 1; i < 4; i++)
{
numbers[i] = numbers[i + 1];
}
printf("Array after deletion:\n");
for(i = 0; i < 4; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Enter 5 numbers:
10
20
30
40
50
Enter position to delete (1-5): 3
Array after deletion:
10 20 40 50
Explanation
After deleting an element, all subsequent elements are shifted one position to the left to fill the gap.
Concepts Covered
- Arrays
- Element Deletion
- Array Shifting
- Loop
Chapter Summary
In this chapter, you learned how to use arrays to store and process multiple values efficiently. You practiced reading and printing array elements, finding sums, averages, largest and smallest values, searching, sorting, reversing, merging arrays, identifying duplicates, deleting elements, and solving practical array-based programming problems. Arrays are a fundamental concept that forms the basis for advanced data structures and algorithms in C programming.
Key Takeaways
- Arrays store multiple values of the same data type.
- Array indexing starts from 0.
- Loops are commonly used to traverse arrays.
- Arrays simplify data storage and processing.
- Searching techniques help locate specific elements.
- Sorting arranges data in ascending or descending order.
- Nested loops are often used for comparison-based operations.
- Array manipulation includes copying, merging, reversing, and deleting elements.
- Arrays are widely used in algorithms and real-world software.
- A strong understanding of arrays is essential before learning strings, pointers, and dynamic memory allocation.
Frequently Asked Questions (FAQs)
1. What is an array in C?
An array is a collection of elements of the same data type stored in contiguous memory locations.
2. Why are arrays used in C programming?
Arrays allow multiple related values to be stored in a single variable, making programs more organized and efficient.
3. How does array indexing work in C?
Array indexing starts at 0, meaning the first element is accessed using index 0.
4. Can arrays store different data types?
No. A single array can only store elements of one data type, such as int, float, or char.
5. How do you find the length of an array?
The number of elements is determined when the array is declared. For example:
int numbers[5];
This array contains 5 elements.
6. What is array traversal?
Array traversal means accessing each element of an array, usually using a loop.
7. What is the difference between searching and sorting?
- Searching finds the position of a specific element.
- Sorting arranges elements in ascending or descending order.
8. Why are arrays important in programming?
Arrays are used extensively in data processing, searching, sorting, mathematical computations, databases, operating systems, games, embedded systems, and many other real-world applications. They are one of the most fundamental data structures in C programming.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
