# Counts the number of odd integers in x (the argument vector)
#First define the function countoddnum
countoddnum <- function(x) {
#k (a counter) is used to count the number of odd numbers in x.
k <- 0
#In each iteration, n takes the value of the corresponding element x
#In test case one below , the loop iterates 7 times because the vector x has 7 elements.
for (n in x) {
if (n %% 2 == 1) k <- k+1
}
return(k)
}
#Then call the function on a couple of test cases
#Test case one
#It can be seen that x contains 4 odd numbers for test case one
countoddnum(c(1,3,5,23,22,44,66))
## [1] 4
#Test case two
countoddnum(c(1,2,3,7,9,78,90,115))
## [1] 5
#Try this: alter the function to count even numbers
#Answer: if(n %% 2==0)