📌  相关文章
📜  计算数组中偶数和奇数长度元素的数量

📅  最后修改于: 2021-09-08 12:39:11             🧑  作者: Mango

给定一个大小为N的整数数组arr[] ,任务是找到具有偶数和奇数长度的数组元素。
例子:

方法:要计算偶数或奇数的位数,将每个数字转换为一个字符串。然后检查长度是奇数还是偶数。最后,分别打印偶数长度和奇数长度的计数。
下面是上述方法的实现:

CPP
// C++ program to find the count
// number of even and odd
// length elements in an Array
 
#include 
using namespace std;
 
// Function to find the number elements of
// the array having even length and odd.
void EvenOddLength(int arr[], int n)
{
    // Store numbers with even length
    int even = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Conversion of integer to string
        string x = to_string(arr[i]);
 
        if (x.length() % 2 == 0)
            even++;
    }
 
    cout << "Number of even "
         << "length elements = "
         << even << endl;
    cout << "Number of odd "
         << "length elements = "
         << n - even << endl;
}
 
// Driver code
int main()
{
    int arr[] = { 12, 44, 213, 232, 3433 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    EvenOddLength(arr, n);
 
    return 0;
}


Java
// Java program to find the count
// number of even and odd
// length elements in an Array
import java.util.*;
class GFG{
 
// Function to find the number elements of
// the array having even length and odd.
    static void EvenOddLength(int arr[], int n)
    {
        // Store numbers with even length
        int even = 0;
     
        for (int i = 0; i < n; i++) {
     
            // Conversion of integer to string
            String x = Integer.toString(arr[i]);
     
            if (x.length() % 2 == 0)
                even++;
        }
     
        System.out.println("Number of even length elements = "+even);
        System.out.println("Number of odd length elements = "+(n - even));
    }
     
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 12, 44, 213, 232, 3433 };
        int n = arr.length;
     
        // Function call
        EvenOddLength(arr, n);
     
    }
}
 
// This code is contributed by AbhiThakur


Python3
# Python3 program to find the count
# number of even and odd
# length elements in an Array
 
# Function to find the number elements of
# the array having even length and odd.
def EvenOddLength(arr, n):
     
    # Store numbers with even length
    even = 0
 
    for i in range(n):
 
        # Conversion of integer to string
        x = str(arr[i])
 
        if (len(x) % 2 == 0):
            even += 1
 
    print( "Number of even length elements = ", even)
    print( "Number of odd length elements = ", n - even)
 
# Driver code
if __name__ == '__main__':
    arr= [12, 44, 213, 232, 3433]
    n = len(arr)
 
    # Function call
    EvenOddLength(arr, n)
 
# This code is contributed by mohit kumar 29


C#
// C# program to find the count
// number of even and odd
// length elements in an Array
using System;
 
class GFG{
  
// Function to find the number elements of
// the array having even length and odd.
    static void EvenOddLength(int []arr, int n)
    {
        // Store numbers with even length
        int even = 0;
      
        for (int i = 0; i < n; i++) {
      
            // Conversion of integer to string
            String x = arr[i].ToString();
      
            if (x.Length % 2 == 0)
                even++;
        }
      
        Console.WriteLine("Number of even length elements = "+even);
        Console.WriteLine("Number of odd length elements = "+(n - even));
    }
      
    // Driver code
    public static void Main(String []args)
    {
        int []arr = { 12, 44, 213, 232, 3433 };
        int n = arr.Length;
      
        // Function call
        EvenOddLength(arr, n);
      
    }
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
Number of even length elements = 3
Number of odd length elements = 2

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live