Top 30+ Array Example in C

Array example in C programming language with code and output. An array is a variable that can store multiple values. 

C Example on Single dimensional Array

1) Find Length of Array in C:- Write the C program to find the length of the given array and use this length value to display the array. Examples:-

Array = {10, 20, 30, 40, 50};
Length of array = 5

Array = {‘K’, ‘n’, ‘o’, ‘w’, ‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’};
Length of array = 11


2) Print an Array in C:- Write C program to print or display an array in C. What are the different ways to print or display an array? How to use length value for this purpose.


3) Sum of array elements in C:- Write a C program to calculate the sum of array elements and display it to the screen. Example:-

Array = {10, 20, 30, 40, 50}
Then, Sum of array elements = 150


4) Display even and odd numbers in an array:- Write a C program to find and display even and odd numbers in an array. Example:-

Array: {10, 15, 20, 54, 26}
Even numbers: 10 20 54 26
Odd numbers: 15


5) Sum and count of even and odd numbers in an array:- Write a C program to count the even and odd numbers in an array, calculate the sum of the array elements. Example:-

Array = {1, 2, 3, 4, 5}
Total Sum: 15
Total Even numbers: 2
Odd numbers = 3


6) Count the positive, negative, and zeros in an array:- Write a C program to count the positive negative and zeros in given array. Example:-

Array: { 10, 20, 0, -5, -9 }
Positive number count = 2
Negative number count = 2
Zeros count = 1


7) Sum of positive and negative numbers in an array:- Write a C program to find the sum of all negative numbers and the sum of all positive numbers. Also, find the sum of them. Example:-

Array = 9 -6 5 0 -2
All Positive numbers sum: 14
All Negative numbers sum: -8
Total sum = 6


8) Find the average of array elements and numbers greater than average in the array:- Write a C program to find the average of the array elements and also find the elements which are greater than the average value. Example:-

Array = {10, 20, 5, 15, 30}
Sum=80 Average=16.00
Numbers greater than average are: 20 30


9) Smallest and largest array element with their position:- Write a C program to find the smallest and largest array element with their position in the given array. Example:-

Array = { 50, 9, 5, 10 }
The largest element is 50 at 0 indexes.
The smallest element is 5 at 2 indexes.


10) First max and min, Second max and min number in an array:- Write a program to find the first maximum, first minimum, second maximum, and second minimum in the given list of N numbers (array). Example:-

Array = { 1, 2, 3, 4, 5 }
First maximum = 5
Second maximum = 4
First minimum = 1
Second minimum = 2


11) Count Repeated Elements in Array:- Write a C program to count the repeated elements in the given array. Example:- In the below example 20, and 10 occurs multiple times.

Array = { 50, 20, 10, 40, 20, 10, 10, 60, 30, 70 };
The number of Repeated elements are = 2


12) Find Duplicate Elements in Array:- Write a C program to find and display the duplicate elements in an array. Example:-

Array = { 50, 20, 10, 40, 20, 10, 10, 60, 30, 70 };
Repeated elements are: 20 10



13) Search an array element and display its index:- Write a C program to search an element from the list of numbers. If an element found then display it with the position in the array. Example:-

Array = { 5, 20, 9, 85, 4 }
Enter element to search: 20
20 found at position 2


14) Search position of Nth times occurred element in an array:- Write a program to search elements occurrence Nth times in an array. If an element found only after that it should ask the user to enter the occurrence of an element. Example:- Array = { 5, 6, 2, 5, 9, 6, 5 }

Enter element to search: 8
8 not found.

Enter element to search: 5
Enter times of occurrence of element: 3
5 found 3 times at 6 indexes.


15) C program to sort an array of numbers and display:- Write a C program to sort an array of numbers and display it to the screen. Example:-

Array: {90, 23, 5, 60, 40}
Sorted Array: {5, 23, 40, 60, 90}


16) Reverse an Array in C:- Write a C program to find the reverse of an array. To find the reverse of an array, you can take the help of another array or you can reverse in the same array. Example:-

Original array: 15 25 35 40 50
The reverse of the array: 50 40 35 25 15


17) C program to Copy an Array:- Write a C program to copy an array and display it. Example:-

Original array = {10, 20, 30, 40, 50};
Copied array = {10, 20, 30, 40, 50};


18) Insert an element in an array:- write a C program to insert a new element entered from the user at a particular position in a given list of numbers. Example:-

Array = {10, 25, 15, 14, 30}
Position = 2
Element = 6
Resultant array = {10, 25, 6, 15, 14, 30}


19) Delete an element in an array:- Write a C program to delete/remove an element from an array using position. Example:-

Array: {10, 20, 30, 40, 50}
Enter index: 2
New Array: {10, 20, 40, 50}


20) Merge Two Arrays in C:- Write a C program to merge two arrays. First write program to merge without sorting, then write another program to merge with sorting. Example:-

Example of merging of two int arrays without sorting,
Array1 = {10, 20, 30, 40, 50}
Array2 = {9, 18, 27, 36, 45}
Then the Result should be,
Merged Array = {10, 20, 30, 40, 50, 9, 18, 27, 36, 45}

Example of merging of two int arrays in ascending order,
Array1 = {45, 18, 36, 27, 9}
Array2 = {40, 10, 20, 30, 50}
Then the Result should be,
Merged Array = {9, 10, 18, 20, 27, 30, 36, 40, 45, 50}


21) Merge Two Sorted Arrays in C:- Two sorted arrays are given, write a C program to merge them in sorted order. Example:-

Example of merging of two int arrays in ascending order,
Array1 = {1, 9, 10, 14, 15}
Array2 = {2, 3, 5, 11, 12}
Then the Result should be,
Merged Array = {1, 2, 3, 5, 9, 10, 11, 12, 14, 15}


Searching Algorithm in C

22) Linear Search in C:- Write a C program to implement the linear search algorithm. It is used to search an element in an array. Example:-

Array = {50, 90, 30, 70, 60};
Element to Search = 30
Output:- 30 found at Index 2.


23) Binary Search in C:- Write a C program to implement the binary search algorithm. It is used to search an element in the sorted array. If the array is unsorted then the binary search algorithm is not applicable. Example:-

Array = {10, 20, 30, 40, 50}
Element to Search = 40
Output:- 40 found at Index 3.


C Example of Multidimensional Array

24) Print or display 2D Array in C:- Write a C program to display a two-dimensional (2D) array.


25) Store temperature of two Cities for a week and display:- Write a C program to store the display temperature of two cities for a week and then display it. It is a C array example of how to take a two-dimensional array as input and display it.


26) Pass a multidimensional array to a function:- Write a C program to demonstrate how to pass a multidimensional array to a function.


27) Find largest and smallest in a 2D array with position:- Write a C program to find the largest and smallest in a 2D array of numbers with their position or location. Take an array of numbers as input, find the largest and smallest element among them, and display the result. Example:-

2D Array:
4 5 6
9 8 7
2 3 5
Largest element in array is 9 in location arr[1][0]
Smallest element in array is 2 in location arr[2][0]


C Program Examples on Matrix

matrix

28) Take and Print Matrix in C:- Write a C program to take a matrix as input and display it. The matrix can be square matrix-like 2×2, 3×3, 4×4 or non-square matrix-like 2×3, 3×2, 2×4, 4×3 and e.t.c. Therefore ask the user to enter row and column size.


29) Matrix Addition in C:- Write a C program to perform matrix addition operation on two matrices. Example:-

Matrix1 = { { 5, 6 }, { 7, 8 } }
Matrix2 = { { 1, 2 }, { 3, 4 } }
Resultant Matrix =
6 8
10 12


30) Subtraction of Two Matrix in C:- Write a C program to perform matrix subtraction operation on two matrices. Example:-

Matrix1 = { { 6, 5 }, { 9, 5 } }
Matrix2 = { { 1, 2 }, { 3, 4 } }
Resultant Matrix =
5 3
6 1


31) Matrix Multiplication in C:- Write a program for matrix multiplication in C using the function. Example:-

Matrix1 = { { 5, 6 }, { 9, 8 } }
Matrix2 = { { 1, 3 }, { 3, 2 } }
Resultant Matrix:
23 27
33 43


32) Transpose of a Matrix in C:- Write a C program to find the transpose of a matrix. Example:-

Matrix =
1 2 3
4 5 6
7 8 9
Transpose of Matrix =
1 4 7
2 5 8
3 6 9


33) Sum of Diagonal Elements of a Matrix in C:-  Write a program to find the sum of diagonal elements of a matrix in C. Example:-

Matrix =
1 2 3
4 5 6
7 8 9
Sum of diagonal elements = 15


34) Row Sum and Column Sum of a Matrix in C:- Write a C program to find the sum of each row and column of a matrix. Example:-

Matrix:
1 2 3
4 5 6
7 8 9
Sum of row 0 = 6, Sum of Column 0 = 12
Sum of row 1 = 15, Sum of Column 1 = 15
Sum of row 2 = 24, Sum of Column 2 = 18


35) Matrix Operations in C – Addition, Subtraction, Multiplication, Transpose:- Write a C program to perform various matrix operations addition, subtraction, multiplication, transpose using switch-case statement and function. It is an menu-driven program where end-user can choose to perform add, subtract, multiply or transpose operation on the matrix. Example:-

First Matrix:
5 6 7
8 9 10
3 1 2
Second Matrix:
1 2 3
4 5 6
7 8 9

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 1
Sum of matrix:
6 8 10
12 14 16
10 9 11

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 2
Subtraction of matrix:
4 4 4
4 4 4
-4 -7 -7

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 3
Multiplication of matrix:
78 96 114
114 141 168
21 27 33

Choose the matrix operation,
----------------------
1. Addition
2. Subtraction
3. Multiplication
4. Transpose
5. Exit
----------------------
Enter your choice: 5
Thank You.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *