Introduction
A one-dimensional array in C is used to store multiple values of the same data type under one variable name. Instead of creating separate variables for every value, an array lets you store related data in a sequence and access each value using an index. In this chapter, you will practice 1D arrays through beginner-friendly examples covering input, output, sum, average, largest and smallest values, searching, counting, and reversing an array. One Dimensional Arrays in C Practice questions with solutions to help you understand the concepts.
Q1. Create and Print a One-Dimensional Array
Problem Statement
Write a C program to create an integer array containing five numbers and print all its elements.
C Program
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int i;
for (i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
10 20 30 40 50
Explanation
This statement creates an array:
int numbers[5];
The number 5 means the array can store five integers.
The array is initialized as:
int numbers[5] = {10, 20, 30, 40, 50};
Array indexing starts from 0.
So the positions are:
Index Value
0 10
1 20
2 30
3 40
4 50
The loop accesses every element using:
numbers[i]
Concepts Covered
- One-dimensional array
- Array declaration
- Array initialization
- Array indexing
forloop
Q2. Take 5 Array Elements from the User
Problem Statement
Write a C program that accepts five integers from the user and then prints them.
C Program
#include <stdio.h>
int main()
{
int numbers[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
printf("\nArray elements:\n");
for (i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Enter number 1: 10
Enter number 2: 25
Enter number 3: 15
Enter number 4: 40
Enter number 5: 30
Array elements:
10 25 15 40 30
Explanation
First, we declare:
int numbers[5];
Then the first for loop takes five values.
Notice:
scanf("%d", &numbers[i]);
The value is stored directly at the current array index.
For example:
numbers[0] → first value
numbers[1] → second value
numbers[2] → third value
The second loop prints all the stored values.
Concepts Covered
- Array input
- Array indexing
scanf()forloop
Q3. Find the Sum of Array Elements
Problem Statement
Write a C program to find the sum of all elements in an integer array.
C Program
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int i;
int sum = 0;
for (i = 0; i < 5; i++)
{
sum = sum + numbers[i];
}
printf("Sum = %d", sum);
return 0;
}
Sample Output
Sum = 150
Explanation
We start with:
int sum = 0;
Then every array element is added:
10 + 20 + 30 + 40 + 50
The loop performs:
sum = sum + numbers[i];
The final result is:
150
Concepts Covered
- Array traversal
- Sum of elements
forloop- Accumulator variable
Q4. Calculate the Average of Array Elements
Problem Statement
Write a C program to calculate the average of five numbers stored in an array.
C Program
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int i;
int sum = 0;
float average;
for (i = 0; i < 5; i++)
{
sum = sum + numbers[i];
}
average = sum / 5.0;
printf("Average = %.2f", average);
return 0;
}
Sample Output
Average = 30.00
Explanation
First, we calculate the total:
10 + 20 + 30 + 40 + 50 = 150
Then:
Average = 150 / 5
= 30
We use:
5.0
instead of:
5
so that the calculation is performed using floating-point arithmetic.
Concepts Covered
- Array traversal
- Sum
- Average
float- Arithmetic operations
Q5. Find the Largest Element in an Array
Problem Statement
Write a C program to find the largest number in an array.
C Program
#include <stdio.h>
int main()
{
int numbers[5] = {25, 70, 15, 90, 40};
int i;
int largest;
largest = numbers[0];
for (i = 1; i < 5; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
printf("Largest = %d", largest);
return 0;
}
Sample Output
Largest = 90
Explanation
We initially assume that the first element is the largest:
largest = numbers[0];
So:
largest = 25
Then we compare the remaining elements.
70 > 25 → largest = 70
15 > 70 → no change
90 > 70 → largest = 90
40 > 90 → no change
The final answer is:
90
Concepts Covered
- Array traversal
- Largest element
ifstatement- Comparison
Q6. Find the Smallest Element in an Array
Problem Statement
Write a C program to find the smallest number in an array.
C Program
#include <stdio.h>
int main()
{
int numbers[5] = {25, 70, 15, 90, 40};
int i;
int smallest;
smallest = numbers[0];
for (i = 1; i < 5; i++)
{
if (numbers[i] < smallest)
{
smallest = numbers[i];
}
}
printf("Smallest = %d", smallest);
return 0;
}
Sample Output
Smallest = 15
Explanation
We initially store the first element as the smallest:
smallest = numbers[0];
Then every remaining element is compared with it.
The smallest value found is:
15
This is almost the same logic as finding the largest value. The only major difference is the comparison operator:
numbers[i] < smallest
Concepts Covered
- Array traversal
- Smallest element
- Comparison
if
Q7. Count Even and Odd Numbers in an Array
Problem Statement
Write a C program to count how many even and odd numbers are present in an array.
C Program
#include <stdio.h>
int main()
{
int numbers[6] = {10, 15, 20, 25, 30, 35};
int i;
int even = 0;
int odd = 0;
for (i = 0; i < 6; i++)
{
if (numbers[i] % 2 == 0)
{
even++;
}
else
{
odd++;
}
}
printf("Even numbers = %d\n", even);
printf("Odd numbers = %d", odd);
return 0;
}
Sample Output
Even numbers = 3
Odd numbers = 3
Explanation
The % operator checks whether a number is divisible by 2.
For example:
10 % 2 = 0 → Even
15 % 2 = 1 → Odd
Whenever an even number is found:
even++;
For an odd number:
odd++;
The array contains:
10, 20, 30 → 3 even numbers
15, 25, 35 → 3 odd numbers
Concepts Covered
- Array traversal
if-else- Modulus operator
- Counting
Q8. Search for an Element in an Array
Problem Statement
Write a C program to search for a particular number in an array and display whether it exists.
C Program
#include <stdio.h>
int main()
{
int numbers[6] = {10, 25, 30, 45, 50, 60};
int search;
int i;
int found = 0;
printf("Enter number to search: ");
scanf("%d", &search);
for (i = 0; i < 6; i++)
{
if (numbers[i] == search)
{
found = 1;
break;
}
}
if (found == 1)
{
printf("Number found.");
}
else
{
printf("Number not found.");
}
return 0;
}
Sample Output
Enter number to search: 45
Number found.
Explanation
The user enters a number to search.
Suppose:
search = 45
The loop checks:
10 == 45 → No
25 == 45 → No
30 == 45 → No
45 == 45 → Yes
When the number is found:
found = 1;
break;
The break statement stops the search because there is no need to check the remaining elements.
Concepts Covered
- Array searching
ifbreak- Boolean-style flag
- Array traversal
Q9. Print an Array in Reverse Order
Problem Statement
Write a C program to print the elements of an array in reverse order.
C Program
#include <stdio.h>
int main()
{
int numbers[5] = {10, 20, 30, 40, 50};
int i;
printf("Array in reverse order:\n");
for (i = 4; i >= 0; i--)
{
printf("%d ", numbers[i]);
}
return 0;
}
Sample Output
Array in reverse order:
50 40 30 20 10
Explanation
The array contains five elements.
Their indexes are:
0 1 2 3 4
The last element is at index 4.
Therefore, the loop starts from:
i = 4
and moves backward:
4 → 3 → 2 → 1 → 0
The values are printed as:
50 40 30 20 10
Concepts Covered
- Array indexing
- Reverse traversal
forloop- Decrement
Q10. Copy One Array into Another Array
Problem Statement
Write a C program to copy all elements from one array into another array.
C Program
#include <stdio.h>
int main()
{
int original[5] = {10, 20, 30, 40, 50};
int copy[5];
int i;
for (i = 0; i < 5; i++)
{
copy[i] = original[i];
}
printf("Copied array:\n");
for (i = 0; i < 5; i++)
{
printf("%d ", copy[i]);
}
return 0;
}
Sample Output
Copied array:
10 20 30 40 50
Explanation
We have two arrays:
int original[5] = {10, 20, 30, 40, 50};
int copy[5];
The loop copies each element:
copy[i] = original[i];
The process is:
original[0] → copy[0]
original[1] → copy[1]
original[2] → copy[2]
original[3] → copy[3]
original[4] → copy[4]
The second array now contains the same values.
Concepts Covered
- Two arrays
- Array copying
- Indexing
forloop
Key Takeaways
- A one-dimensional array stores multiple values of the same data type.
- Array indexing in C starts from
0. - For an array of size
5, valid indexes are0to4. - Array elements are accessed using the index operator
[]. - A
forloop is commonly used to traverse an array. - Array elements can be initialized when the array is declared.
- User input can be stored directly into array elements using
scanf(). - Arrays make it easier to work with collections of related values.
- Common array operations include sum, average, searching, counting, reversing, and finding minimum or maximum values.
- Accessing an array outside its valid bounds can lead to undefined behavior.
- A one-dimensional array is the foundation for understanding two-dimensional and multidimensional arrays.
- Arrays will become even more useful when combined with functions, pointers, and strings.
FAQs
1. What is a one-dimensional array in C?
A one-dimensional array is a collection of elements of the same data type stored under one variable name.
Example:
int numbers[5];
This array can store five integers.
2. What is the index of the first element in a C array?
The first element has index 0.
For example:
int numbers[3] = {10, 20, 30};
Here:
numbers[0] = 10
3. How do you declare an array in C?
The general syntax is:
data_type array_name[size];
Example:
int marks[5];
4. How do you take input into an array in C?
You can use a loop with scanf():
for (int i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
5. How do you print all elements of an array?
Use a loop:
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
6. Can an array store different data types in C?
No. A normal C array stores elements of one declared data type.
For example:
int numbers[5];
stores integers.
A float array stores floating-point values:
float prices[5];
7. What is array traversal in C?
Array traversal means visiting or processing each element of an array, usually with a loop.
For example:
for (int i = 0; i < 5; i++)
{
printf("%d ", numbers[i]);
}
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
