Program to count even and odd numbers in an array
Here given code implementation process.
/*
C Program
Program to count even and odd numbers in an array
*/
#include <stdio.h>
void countEvenOdd(int arr[], int n)
{
// Set the initial value even and odd counter
int even = 0;
int odd = 0;
printf(" Array :");
for (int i = 0; i < n; ++i)
{
if(arr[i] % 2 == 0)
{
// When number is even
even++;
}
else
{
// When number is odd
odd++;
}
// Display array element
printf(" %d",arr[i]);
}
// Display calculated result
printf("\n Even : %d",even);
printf("\n Odd : %d\n",odd);
}
int main()
{
int arr[] = {3,7,2,9,4,8,-1,3};
// Get the number of elements in array
int n = sizeof(arr) / sizeof(arr[0]);
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
countEvenOdd(arr,n);
return 0;
}
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
// Java Program for
// Program to count even and odd numbers in an array
public class Counting
{
public void countEvenOdd(int[] arr, int n)
{
// Set the initial value even and odd counter
int even = 0;
int odd = 0;
System.out.print(" Array :");
for (int i = 0; i < n; ++i)
{
if (arr[i] % 2 == 0)
{
// When number is even
even++;
}
else
{
// When number is odd
odd++;
}
// Display array element
System.out.print(" " + arr[i]);
}
// Display calculated result
System.out.println("\n Even : " + even);
System.out.println(" Odd : " + odd);
}
public static void main(String[] args)
{
Counting task = new Counting();
int[] arr = {
3 , 7 , 2 , 9 , 4 , 8 , -1 , 3
};
// Get the number of elements in array
int n = arr.length;
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
task.countEvenOdd(arr, n);
}
}
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
package main
import "fmt"
// Go Program for
// Program to count even and odd numbers in an array
type Counting struct {}
func getCounting() * Counting {
var me *Counting = &Counting {}
return me
}
func(this Counting) countEvenOdd(arr[] int, n int) {
// Set the initial value even and odd counter
var even int = 0
var odd int = 0
fmt.Print(" Array :")
for i := 0 ; i < n ; i++ {
if arr[i] % 2 == 0 {
// When number is even
even++
} else {
// When number is odd
odd++
}
// Display array element
fmt.Print(" ", arr[i])
}
// Display calculated result
fmt.Println("\n Even : ", even)
fmt.Println(" Odd : ", odd)
}
func main() {
var task * Counting = getCounting()
var arr = [] int {
3,
7,
2,
9,
4,
8,
-1,
3,
}
// Get the number of elements in array
var n int = len(arr)
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
task.countEvenOdd(arr, n)
}
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
// Include header file
#include <iostream>
using namespace std;
// C++ Program for
// Program to count even and odd numbers in an array
class Counting
{
public: void countEvenOdd(int arr[], int n)
{
// Set the initial value even and odd counter
int even = 0;
int odd = 0;
cout << " Array :";
for (int i = 0; i < n; ++i)
{
if (arr[i] % 2 == 0)
{
// When number is even
even++;
}
else
{
// When number is odd
odd++;
}
// Display array element
cout << " " << arr[i];
}
// Display calculated result
cout << "\n Even : " << even << endl;
cout << " Odd : " << odd << endl;
}
};
int main()
{
Counting *task = new Counting();
int arr[] = {
3 , 7 , 2 , 9 , 4 , 8 , -1 , 3
};
// Get the number of elements in array
int n = sizeof(arr) / sizeof(arr[0]);
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
task->countEvenOdd(arr, n);
return 0;
}
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
// Include namespace system
using System;
// Csharp Program for
// Program to count even and odd numbers in an array
public class Counting
{
public void countEvenOdd(int[] arr, int n)
{
// Set the initial value even and odd counter
int even = 0;
int odd = 0;
Console.Write(" Array :");
for (int i = 0; i < n; ++i)
{
if (arr[i] % 2 == 0)
{
// When number is even
even++;
}
else
{
// When number is odd
odd++;
}
// Display array element
Console.Write(" " + arr[i]);
}
// Display calculated result
Console.WriteLine("\n Even : " + even);
Console.WriteLine(" Odd : " + odd);
}
public static void Main(String[] args)
{
Counting task = new Counting();
int[] arr = {
3 , 7 , 2 , 9 , 4 , 8 , -1 , 3
};
// Get the number of elements in array
int n = arr.Length;
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
task.countEvenOdd(arr, n);
}
}
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
<?php
// Php Program for
// Program to count even and odd numbers in an array
class Counting
{
public function countEvenOdd($arr, $n)
{
// Set the initial value even and odd counter
$even = 0;
$odd = 0;
echo(" Array :");
for ($i = 0; $i < $n; ++$i)
{
if ($arr[$i] % 2 == 0)
{
// When number is even
$even++;
}
else
{
// When number is odd
$odd++;
}
// Display array element
echo(" ".$arr[$i]);
}
// Display calculated result
echo("\n Even : ".$even.
"\n");
echo(" Odd : ".$odd.
"\n");
}
}
function main()
{
$task = new Counting();
$arr = array(3, 7, 2, 9, 4, 8, -1, 3);
// Get the number of elements in array
$n = count($arr);
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
$task->countEvenOdd($arr, $n);
}
main();
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
// Node JS Program for
// Program to count even and odd numbers in an array
class Counting
{
countEvenOdd(arr, n)
{
// Set the initial value even and odd counter
var even = 0;
var odd = 0;
process.stdout.write(" Array :");
for (var i = 0; i < n; ++i)
{
if (arr[i] % 2 == 0)
{
// When number is even
even++;
}
else
{
// When number is odd
odd++;
}
// Display array element
process.stdout.write(" " + arr[i]);
}
// Display calculated result
console.log("\n Even : " + even);
console.log(" Odd : " + odd);
}
}
function main()
{
var task = new Counting();
var arr = [3, 7, 2, 9, 4, 8, -1, 3];
// Get the number of elements in array
var n = arr.length;
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
task.countEvenOdd(arr, n);
}
main();
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
# Python 3 Program for
# Program to count even and odd numbers in an array
class Counting :
def countEvenOdd(self, arr, n) :
# Set the initial value even and odd counter
even = 0
odd = 0
print(" Array :", end = "")
i = 0
while (i < n) :
if (arr[i] % 2 == 0) :
# When number is even
even += 1
else :
# When number is odd
odd += 1
# Display list element
print(" ", arr[i], end = "")
i += 1
# Display calculated result
print("\n Even : ", even)
print(" Odd : ", odd)
def main() :
task = Counting()
arr = [3, 7, 2, 9, 4, 8, -1, 3]
# Get the number of elements in list
n = len(arr)
# Test
# arr[] = [3,7,2,9,4,8,-1,3]
# odd = [3,7,9,-1,3] count 5
# even = [2,4,8] count 3
# Count even and odd elements
task.countEvenOdd(arr, n)
if __name__ == "__main__": main()
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
# Ruby Program for
# Program to count even and odd numbers in an array
class Counting
def countEvenOdd(arr, n)
# Set the initial value even and odd counter
even = 0
odd = 0
print(" Array :")
i = 0
while (i < n)
if (arr[i] % 2 == 0)
# When number is even
even += 1
else
# When number is odd
odd += 1
end
# Display array element
print(" ", arr[i])
i += 1
end
# Display calculated result
print("\n Even : ", even, "\n")
print(" Odd : ", odd, "\n")
end
end
def main()
task = Counting.new()
arr = [3, 7, 2, 9, 4, 8, -1, 3]
# Get the number of elements in array
n = arr.length
# Test
# arr[] = [3,7,2,9,4,8,-1,3]
# odd = [3,7,9,-1,3] count 5
# even = [2,4,8] count 3
# Count even and odd elements
task.countEvenOdd(arr, n)
end
main()
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
// Scala Program for
// Program to count even and odd numbers in an array
class Counting()
{
def countEvenOdd(arr: Array[Int], n: Int): Unit = {
// Set the initial value even and odd counter
var even: Int = 0;
var odd: Int = 0;
print(" Array :");
var i: Int = 0;
while (i < n)
{
if (arr(i) % 2 == 0)
{
// When number is even
even += 1;
}
else
{
// When number is odd
odd += 1;
}
// Display array element
print(" " + arr(i));
i += 1;
}
// Display calculated result
println("\n Even : " + even);
println(" Odd : " + odd);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Counting = new Counting();
var arr: Array[Int] = Array(3, 7, 2, 9, 4, 8, -1, 3);
// Get the number of elements in array
var n: Int = arr.length;
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
task.countEvenOdd(arr, n);
}
}
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
import Foundation;
// Swift 4 Program for
// Program to count even and odd numbers in an array
class Counting
{
func countEvenOdd(_ arr: [Int], _ n: Int)
{
// Set the initial value even and odd counter
var even: Int = 0;
var odd: Int = 0;
print(" Array :", terminator: "");
var i: Int = 0;
while (i < n)
{
if (arr[i] % 2 == 0)
{
// When number is even
even += 1;
}
else
{
// When number is odd
odd += 1;
}
// Display array element
print(" ", arr[i], terminator: "");
i += 1;
}
// Display calculated result
print("\n Even : ", even);
print(" Odd : ", odd);
}
}
func main()
{
let task: Counting = Counting();
let arr: [Int] = [3, 7, 2, 9, 4, 8, -1, 3];
// Get the number of elements in array
let n: Int = arr.count;
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
task.countEvenOdd(arr, n);
}
main();
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
// Kotlin Program for
// Program to count even and odd numbers in an array
class Counting
{
fun countEvenOdd(arr: Array < Int > , n: Int): Unit
{
// Set the initial value even and odd counter
var even: Int = 0;
var odd: Int = 0;
print(" Array :");
var i: Int = 0;
while (i < n)
{
if (arr[i] % 2 == 0)
{
// When number is even
even += 1;
}
else
{
// When number is odd
odd += 1;
}
// Display array element
print(" " + arr[i]);
i += 1;
}
// Display calculated result
println("\n Even : " + even);
println(" Odd : " + odd);
}
}
fun main(args: Array < String > ): Unit
{
val task: Counting = Counting();
val arr: Array < Int > = arrayOf(3, 7, 2, 9, 4, 8, -1, 3);
// Get the number of elements in array
val n: Int = arr.count();
// Test
// arr[] = [3,7,2,9,4,8,-1,3]
// odd = [3,7,9,-1,3] count 5
// even = [2,4,8] count 3
// Count even and odd elements
task.countEvenOdd(arr, n);
}
Output
Array : 3 7 2 9 4 8 -1 3
Even : 3
Odd : 5
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment