Program to Find Length of Array

Here we are going to write a program to find length of array. You will find the implementation of this program to three languages below, in c, c++ and python. There are several ways of finding length of array. One way is we can take a counter and iterate through the entire array and count the numbers of array elements. Other is we can use sizeof() functions etc. We will see possible ways of doing this program in the implementation below.

Example,
Input:
array_name = 1, 2, 3, 4

Output:
Length of Array: 4

Program to Find Length of Array in C

Methods to Find Length of Array in C-

Method 1: Counting Elements.
Method 2: Using sizeof() function.
Method 3: Using pointer arithmetic.

Method 1:

#include <stdio.h>

int main(){
    int a[]={1,2,3,4,5,6,7};
    int i=0,c=0;
    while(a[i++]!='\0'){
        c++;
    }
    printf("Length of Array: %d",c);
    return 0;
}

Output-
Length of Array: 7

Method 2:
#include <stdio.h>

int main(){
    int a[]={1,2,3,4,5,6,7};
    int c=sizeof(a)/sizeof(a[0]);
    
    printf("Length of Array: %d",c);
    return 0;
}

Output-
Length of Array: 7

Method 3:
#include <stdio.h>

int main(){
    int a[]={1,2,3,4,5,6,7};
    int c = *(&a + 1) - a;
    
    printf("Length of Array: %d",c);
    return 0;
}

Output-
Length of Array: 7

Program to Find Length of Array in C++

Methods of Finding Length of Array in C++

Method 1: Counting Elements.
Method 2: Using sizeof() function.
Method 3: Using pointer arithmetic.
Method 4: Using size() function.
Method 5: Using begin() and end().

Method 1:
#include <iostream>
using namespace std;

int main(){
    int a[]={1,2,3,4,5,6,7};
    int i=0,c=0;
    while(a[i++]!='\0'){
        c++;
    }
    cout<<"Length of Array: "<<c;
    return 0;
}

Output-
Length of Array: 7
Method 2:
#include <iostream>
using namespace std;

int main(){
    int a[]={1,2,3,4,5,6,7};
    int c=sizeof(a)/sizeof(a[0]);
    
    cout<<"Length of Array: "<<c;
    return 0;
}

Output-
Length of Array: 7

Method 3:
#include <iostream>
using namespace std;

int main(){
    int a[]={1,2,3,4,5,6,7};
    int c = *(&a + 1) - a;
    
    cout<<"Length of Array: "<<c;
    return 0;
}

Output-
Length of Array: 7

Method 4:
#include <iostream>
#include <array>
using namespace std;

int main(){
     array<int,7> a={1,2,3,4,5,6,7};
     cout<<"Length of Array: "<<a.size();
     return 0;
}

Output-
Length of Array: 7

Method 5:
#include <iostream>
#include <array>
using namespace std;

int main(){
    int a[]={1,2,3,4,5,6,7};
    int l=end(a) - begin(a);
    cout<<"Length of Array: "<<l;
    return 0;
}

Output-
Length of Array: 7

Program to Find Length of Array in Python

Methods of Finding Length of Array in Python

Method 1: Counting Elements.
Method 2: Using len() function.

Method 1:
a=[1,2,3,4,5,6,7]
c=0
for i in a:
    c=c+1
print("Length of Array: ",c)

Output-
Length of Array: 7

Method 2:
a=[1,2,3,4,5,6,7]
l=len(a)
print("Length of Array: ",l)

Output-
Length of Array: 7

So this is the end. In this post, we learnt about various methods of finding length of array in different languages. These are not the only methods, these are just some of the possible ways to find length of array. You can also try out some other approach as per your knowledge and if you wish you can share them here with your fellow programmers in the comment section.

More Array Programs-

• Program to sort an array in ascending and descending order

• Program to convert decimal to binary 

• Program to add two matrices 

• Python program to find sum average max and min of the given matrix

• C Program to find Sum of Array Elements

Post a Comment

If you have any doubts, please let me know

Previous Post Next Post