In this tutorial, we will discuss the concept of C program to count even and odd numbers in an array
In this post, we are going to learn how to count the even and odd numbers from the given array.
First, we must understand how to identify odd or even numbers
What is Even and Odd
When the number is divided by 2 and the balance becomes zero and the above number is called as the even number – eg 2,4,6,8,10
and on the other sides when it is divided by 2 and balance becomes 1 they are called odd numbers or uneven numbers.
In my previous post, I have explained the various approaches to check whether the number is even or odd in C language
here, we will discuss how to count odd and even nunbers from an array in the C programming language using loops
There are three programs given below
The common procedure of this programs
Program 1
The program allows the user to enter the size of the array and receives the elements from the user.
Then, it will count the even and odd numbers from this array using for loop in C
#include <stdio.h> #include <stdlib.h> int main() { int arr[100]; int i,size,odd=0,even=0; //input the size of the array printf("Enter size of the array\n"); scanf("%d",&size);//reads input from user for array size //input the array elements printf("\nEnter elements of the array\n\n"); for(i=0; i<size; i++) { printf("Enter the element arr[%d] :",i); scanf("%d",&arr[i]);//reads input from user for array elements } for(i=0; i<size; i++) { if(arr[i]%2==0) { even++; } else{ odd++; } } printf("\nTotal even numbers of an array :%d\n",even);//display even numbers printf("Total odd numbers of an array :%d",odd);//display odd numbers getch(); return 0; }
When the above code is executed, it produces the following results
Enter the size of the array 10 Enter elements of the array Enter the element arr[0] : 45 Enter the element arr[1] : 65 Enter the element arr[2] : 68 Enter the element arr[3] : 87 Enter the element arr[4] : 98 Enter the element arr[5] : 12 Enter the element arr[6] : 54 Enter the element arr[7] : 66 Enter the element arr[8] : 67 Enter the element arr[9] : 42 Total even numbers of an array :6 Total odd numbers of an array : 4
Program 1
The program allows the user to enter the size of the array and receives the elements from the user.
Then, it will count the even and odd numbers from this array using while loop in C
#include <stdio.h> #include <stdlib.h> int main() { int arr[100]; int i,size,odd=0,even=0; //input the size of the array printf("Enter size of the array\n"); scanf("%d",&size);//reads input from user for array size //input the array elements printf("\nEnter elements of the array\n\n"); i=0; while(i<size) { printf("Enter the element arr[%d] :",i); scanf("%d",&arr[i]);//reads input from user for array elements i++; } i=0; while(i<size) { if(arr[i]%2==0) { even++; } else{ odd++; } i++; } printf("\nTotal even numbers of an array :%d\n",even);//display even numbers printf("Total odd numbers of an array :%d",odd);//display odd numbers getch(); return 0; }
When the above code is executed, it produces the following results
Enter the size of the array 8 Enter elements of the array Enter the element arr[0] : 987 Enter the element arr[1] : 876 Enter the element arr[2] : 765 Enter the element arr[3] : 654 Enter the element arr[4] : 456 Enter the element arr[5] : 653 Enter the element arr[6] : 542 Enter the element arr[7] : 457 Total even numbers of an array :4 Total odd numbers of an array : 4
Program 1
The program allows the user to enter the size of the array and receives the elements from the user.
Then, it will count the even and odd numbers from this array using the do-while loop in C
#include <stdio.h> #include <stdlib.h> int main() { int arr[100]; int i,size,odd=0,even=0; //input the size of the array printf("Enter size of the array\n"); scanf("%d",&size); //input the array elements printf("\nEnter elements of the array\n\n"); i=0; do{ printf("Enter the element arr[%d] :",i); scanf("%d",&arr[i]); i++; }while(i<size); i=0; do{ if(arr[i]%2==0) { even++; } else{ odd++; } i++; }while(i<size); printf("\nTotal even numbers of an array :%d\n",even);//display even numbers printf("Total odd numbers of an array :%d",odd);//display odd numbers getch(); return 0; }
When the above code is executed, it produces the following results
Enter the size of the array 8 Enter elements of the array Enter the element arr[0] : 900 Enter the element arr[1] : 855 Enter the element arr[2] : 254 Enter the element arr[3] : 545 Enter the element arr[4] : 124 Enter the element arr[5] : 578 Enter the element arr[6] : 980 Enter the element arr[7] : 642 Total even numbers of an array :6 Total odd numbers of an array : 2
Similar post
Java code to compute even and odd numbers of an array
C code to compute even and odd numbers of an array
Display even and odd numbers without if statement in C++
Display even and odd numbers without if statement in Java
Display even and odd numbers without if statement in C
C program to find the largest among three numbers using function
Python code to find the largest of three numbers using the function
C++ code to find the largest of three numbers using the function
Java code to find the largest of three numbers
C code to find the largest of three numbers
C++ code to find the largest of three numbers
Python code to find the largest of three numbers
Java program to find the middle of three number using Method
C code to find the middle of three number using the function
C++ code to find the middle of three number using the function
Python code to find the middle of three number using the function
Java code to find middle of three numbers
C code to find middle of three numbers
C++ code to find middle of three numbers
Python code to find middle of three numbers
Java code to find largest of three numbers in an array
Java code to find smallest of three numbers in an array
C code to find the middle of three number using the function
C++ code to find the middle of three number using the function
Suggested for you
Nested if statement in C++ language
The operator in Python language
If statement in Python language
Data types and Variable in Python
Datatype and variables in Java programming language
If statements in Java language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…