Beta Coder

Learn code like Beta Coder.

Breaking

Tuesday, August 18, 2020

C Program to Count Even and Odd Numbers of an Array

COUNT EVEN AND ODD NUMBERS OF AN ARRAY

COUNT EVEN AND ODD NUMBERS OF AN ARRAY
In this program we learn how to calculate no. of Even and Odd numbers of an array provided by user using C.
Example:

 Input : int arr[5] = {3, 4, 5, 6, 7}
 Output: Number of even numbers = 2
               Number of odd numbers = 3

 Input : int arr[5] = {21, 30, 45, 54, 60}
 Output: Number of even numbers = 3
               Number of even numbers = 2
Simple approch: A number is said to be Even if it is completely divisible by 2 and generates a remainder of 0. A number is said to be Odd if when it dividing by 2 and it is leaves 1 as remainder. C program to count even and odd numbers of an array
// Count even and odd numbers of an array
#include <stdio.h>
#include <conio.h>
void main()
{
	int a[20],n,i,e=0,o=0;
	clrscr();
	printf("\n Enter how many numbers : ");
	scanf("%u",&n);
        printf("\n Enter %d numbers : ",n);
        for(i=0; i<n; i++)
            scanf("%d",&a[i]);
        for(i=0; i<n; i++)
            if(a[i]%2==0)
		e++;
	    else
		o++;
        printf("\n Number of even numbers = %d \n Number of even numbers = %d",e,o);        
	getch();
}
OUTPUT:
   Enter how many numbers : 5
   Enter 5 numbers : 21 30 45 54 60
   Number of even numbers = 3
   Number of odd numbers = 2

No comments:

Post a Comment