Counting the number of even and odd elements in a vector using a for loop? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report In this article, we will discuss how to find the number of even and odd elements in a vector with its working example in the R Programming Language using R for loop. Syntax:vector <- c(...) # Replace ... with the vector elementseven_count <- 0odd_count <- 0for (element in vector) { if (element %% 2 == 0) { even_count <- even_count + 1 } else { odd_count <- odd_count + 1 }}cat("Number of even elements:", even_count, "\n")cat("Number of odd elements:", odd_count, "\n")Example 1 R vector <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) even_count <- 0 odd_count <- 0 for (element in vector) { if (element %% 2 == 0) { even_count <- even_count + 1 } else { odd_count <- odd_count + 1 } } cat("Number of even elements:", even_count, "\n") cat("Number of odd elements:", odd_count, "\n") Output: Number of even elements: 5 Number of odd elements: 5Create a vector with the given elements.Initialize a variable even_count to store the count of even elements. Set its initial value to 0.Initialize a variable odd_count to store the count of odd elements. Set its initial value to 0.Start a for loop that iterates through each element in the vector.Check if the current element is even (divisible by 2).If the current element is even, increment the even_count by 1.If the current element is not even (i.e., odd):Increment the odd_count by 1.Print the no. of even and odd count Example 2 R vector <- c(11, 22, 33, 44, 55, 66, 77, 88, 99 ) even_count <- 0 odd_count <- 0 for (element in vector) { if (element %% 2 == 0) { even_count <- even_count + 1 } else { odd_count <- odd_count + 1 } } cat("Number of even elements:", even_count, "\n") cat("Number of odd elements:", odd_count, "\n") Output: Number of even elements: 4 Number of odd elements: 5 Example 3 R vector <- c(111, 221, 331, 441, 552, 662, 771, 881, 991 ) even_count <- 0 odd_count <- 0 for (element in vector) { if (element %% 2 == 0) { even_count <- even_count + 1 } else { odd_count <- odd_count + 1 } } cat("Number of even elements:", even_count, "\n") cat("Number of odd elements:", odd_count, "\n") Output: Number of even elements: 2 Number of odd elements: 7 Comment More infoAdvertise with us Next Article Counting the number of even and odd elements in a vector using a for loop? A anjugaeu01 Follow Improve Article Tags : R Language Similar Reads Count of odd sum Submatrix with odd element count in the Matrix Given a matrix mat[][] of size N x N, the task is to count the number of submatrices with the following properties: The sum of all elements in the submatrix is odd.The number of elements in the submatrix is odd.Examples: Input: mat[][] = {{1, 2, 3}, {7, 5, 9}, {6, 8, 10}}Output: 8Explanation: As her 15 min read Count the specific value in a given vector in R In this article, we will discuss how to find the specific value in a given vector in R Programming Language. For finding the frequency of a given value two approaches can be employed and both of them are discussed below. Method 1: Naive method We can iterate over the vector in R using a for loop an 2 min read Count number of vector values in range with R In this article, we will see how to count the number of vector values present in the given range in R. To achieve this functionality we can follow the following approach. Approach Create vectorSet rangeIterate through the vectorCheck for elements that are within the rangeAdd themDisplay sum Implemen 2 min read Count Odd and Even You are given an array arr[]. Your task is to count the number of even and odd elements. Return first odd count then even count.Examples: Input: arr = [2, 3, 4, 5, 6]Output: 2 3 Explanation: There are two odds[3, 5] and three even[2, 4, 6] present in the array.Input: arr = [22, 32, 42, 52, 62]Output 5 min read Count pairs with Bitwise OR as Even number Given an array A[] of size N. The task is to find the how many pair(i, j) exists such that A[i] OR A[j] is even. Examples: Input : N = 4 A[] = { 5, 6, 2, 8 } Output :3 Explanation : Since pair of A[] = ( 5, 6 ), ( 5, 2 ), ( 5, 8 ), ( 6, 2 ), ( 6, 8 ), ( 2, 8 ) 5 OR 6 = 7, 5 OR 2 = 7, 5 OR 8 = 13 6 O 7 min read Like