# C PROGRAMS
Array Programs
➤ Find Length of Array
➤ How to Print an Array
➤ Sum of Array Elements
➤ Reverse an Array in C
➤ Copy an Array in C
➤ Merge Two Arrays in C
➤ Merge Two Sorted Arrays
➤ Count Repeated Elements
➤ Find Duplicate Elements
➤ Linear Search in C
➤ Binary Search in C
➤ Insert Element in Array
➤ Delete Element in Array
➤ Display odd-even in Array
➤ Sum & Count of Odd-Even
➤ Count +ve, -ve, & 0
➤ Sum of +ve, -ve Numbers
➤ Avg & Numbers > Avg
➤ Smallest & Largest Element
➤ 1st 2nd Max-Min
➤ Sort Array in C
➤ Search element in Array
➤ Search index of Nth times occurred element
➤ Matrix Programs
How to Find the Length of Array in C? To find the length of the array we have to use sizeof() function. The sizeof() function in C calculates the size in bytes of the passed variable or data type.
To calculate the length of array in C, first, calculate the total size of the array and then calculate the size of the data type. After that divide the total size of the array/size of the data type.
Length of array = ( total size of array ) / ( size of data type )
int array[] = {10, 20, 30}; int size = sizeof(array) / sizeof(int);
The size of the data type is platform-dependent, therefore it is better to use the first element of the array.
Length of array = ( total size of the array ) / ( size of the first element of the array )
int array[] = {10, 20, 30}; int size = sizeof(array) / sizeof(array[0]);
Also see:- Quiz on sizeof() function in C, Most Used Sorting Algorithms In Java
Let us demonstrate it through some examples. Program to find the length of array in C.
#include <stdio.h>
int main()
{
// variable
int numbers[] = {10, 20, 30, 40, 50};
// calculate size in bytes
int arraySize = sizeof(numbers);
int intSize = sizeof(numbers[0]);
// length
int length = arraySize / intSize;
printf("ArraySize = %d bytes.\n", arraySize);
printf("IntSize = %d bytes.\n", intSize);
printf("Length of array = %d \n", length);
return 0;
}
Output:-
ArraySize = 20 bytes.
IntSize = 4 bytes.
Length of array = 5
Find Length of Array in C and Display Array Elements
With the help of the length, we can iterate through the array. In the below program first, we will find the length of array in C and then display the array elements.
#include <stdio.h>
int main()
{
// variables
float arr[] = {10.5, 15.9, 25.2, 30.4, 55.59};
// calculate length
int length = sizeof(arr)/sizeof(arr[0]);
printf("Array length = %d \n", length);
// display the array
printf("Array Elements are, \n");
for (int i = 0; i < length; ++i)
{
printf("%.2f ", arr[i]);
}
return 0;
}
Output:-
Array length = 5
Array Elements are,
10.50 15.90 25.20 30.40 55.59
In the previous program, arr is the array of the floating-point data type. Now, let us see another C program to find the length of the char array.
Program to find the length of array in C,
#include <stdio.h>
int main()
{
// variables
char name[]={'K', 'n', 'o', 'w', 'P', 'r', 'o', 'g', 'r', 'a', 'm'};
// calculate length
int length = sizeof(name)/sizeof(name[0]);
printf("Array length = %d \n", length);
// display the array
printf("Array Elements are, \n");
for (int i = 0; i < length; ++i)
{
printf("%c", name[i]);
}
return 0;
}
Output:-
Array length = 11
Array Elements are,
KnowProgram
Also See:-
- C program to find the sum of elements in an array
- Display even and odd numbers in an array
- Sum and count of even and odd numbers in an array
- Count the positive, negative, and zeros in an array
- Sum of positive and negative numbers in an array
- Average and numbers greater than average in array
- Smallest & largest array element with their position
- First max and min, Second max and min number in an array
- C program to sort a list of an array element & display
- C program to search an Array element and display index
- Search position of Nth times occurred element in array
- C program to insert an element in a given array
- C program to delete an element in a given array
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!
Follow Us
Instagram • Twitter • Youtube • Linkedin • Facebook • Pinterest • Telegram