WAP to count odd and even numbers in a given digit using pointer

Write a program to count odd and even numbers in a given digit using pointer

#include<stdio.h>

void evenOdd(int n, int *ec, int *oc) {
	printf("Enter a number: ");
	scanf("%d",&n);
	while(n!=0) {
		if((n%10)%2==0)
			*ec = *ec+1;
		else
			*oc = *oc+1;
		n/=10;
	}
}

int main() {
	int ec=0, oc=0;
	int x=3244;
	evenOdd(x,&ec,&oc);
	printf("Even digit: %d\nOdd digit: %d",ec,oc);
	return 0;
}

Output

Enter a number: 1234890
Even digit: 4
Odd digit: 3

Behind the scene

Even digits: 2, 4, 8, 0 = 4

Odd digits: 1, 3, 9 = 3

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments