Write Program to find number of even and odd elements in an array

09 June 2022

Write Program to find number of even and odd elements in an array


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get an array as input from the user and then count the number of even and odd elements present in the array.

Input

Enter size of array

4

Enter the elements:

1 3 4 5

Output

Number of even elements: 1

Number of odd elements: 3

 

C Program

#include<stdio.h>

int main(){

    int n;

   int arr[n];

   printf("Enter the size of array elements: ");

   scanf("%d",&n);

   printf("Enter the array elements: ");

   for(int i=0;i<n;i++)

   scanf("%d",&arr[i]);

   int even=0, odd=0;

   for(int i=0; i<n; i++){

     if(arr[i]%2==0)

       even++;

     else

       odd++;

   }

   printf("Number of even elements: %d \nNumber of odd elements: %d", even, odd);

}

 

C++ Program

#include<iostream>

using namespace std;

int main(){

    int n;

   cout<<"Enter the size of array elements: ";

   cin>>n;

   int arr[n];

   cout<<"Enter the array elements: ";

   for(int i=0;i<n;i++)

   cin>>arr[i];

   int even=0, odd=0;

   for(int i=0; i<n; i++){

     if(arr[i]%2==0)

       even++;

     else

       odd++;

   }

   cout<<"Number of even elements: "<<even<< "\nNumber of odd elements: "<<odd;

}

 

Java Program

import java.util.Scanner;

class Main{

  public static void main (String[] args)

  {

     int n;

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter the size of the array: ");

    n = sc.nextInt();

    int[]arr = new int[n];

    System.out.println("Enter the array elements: ");

    for(int i = 0; i < n; i++)

    {

        arr[i] = sc.nextInt();

    }

     int even = 0, odd = 0;

     for(int i=0; i<n; i++){

         if((arr[i] % 2 )== 0)

           even += 1;

 

         else

           odd += 1;

     }

     System.out.println("Number of even elements: "+ even);

     System.out.println("Number of odd elements: "+ odd);

  }

}

 

Python Program

n = int(input("Enter size of array: "))

arr = []

print("Enter array elements: ")

for i in range(0,n):

                temp = int(input())

                arr.append(temp)

even = 0

odd = 0

for i in range(0, n):

   if arr[i]%2==0 :

    even += 1

   else:

    odd += 1

print("Number of even elements: ",even )

print("Number of odd elements: ",odd)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023
Related Articles

Ask Us Anything !