FOR FREE YEAR SOLVED

Counts the number of odd and even elements separately from the array

Back to Programming

Description

The array of elements is taken as input from the user. The program counts the number of odd and even elements separately from the array. For performing this, the array is traversed from the first element and two extra variables are taken to store the number of even and odd elements separately.

 

Algorithm

INPUT: Array elements
OUTPUT: the number of odd and even elements of the array
PROCESS:
Step 1: [taking the input]
	Read n [number of elements]
	For i=0 to n-1 repeat
		Read a[i]
	[end of ‘for’ loop]
Step 2: [finding the number of odd and even elements]
	Set odd<-0
	Set even<-0
	For i=0 to n-1 repeat
		If a[i] mod 2=0 then
			Set even<-even+1
		else
			Set odd<-odd+1
		[End of ‘if’]
	[End of ‘for’ loop]
	Print even,odd
Step 3: Stop.

Code

Complexity: