**Introduction**

This article explores a C program to count even, odd, and zero digits in a number.

By understanding the logic and implementation of this program, you can enhance your programming skills and tackle similar challenges effectively.

Also Read: C Program To Read Two Files Simultaneously

In the world of programming, it is essential to have the ability to manipulate numbers efficiently. One common task is to count the occurrences of even, odd, and zero digits in a given number.

So, let’s dive in and explore the intricacies of the C program to count even, odd, and zero digits in a number.

Also Read: Armstrong Number in C Programming

**C Program to Count Even, Odd, and Zero Digits in a Number**

Before we delve into the implementation details, let’s take a moment to understand the problem at hand.

The task is to develop a C program that counts the number of even, odd, and zero digits in a given number.

Also Read: C Program to Find the Inverse of 2×2 Matrix

This program will help us analyze the distribution of these digits and gain insights into the number’s characteristics.

By following the steps outlined below, we can achieve the desired functionality:

- Accept the input number from the user.
- Initialize counters for even, odd, and zero digits.
- Iterate through each digit in the number.
- Check if the digit is even, odd, or zero.
- Increment the respective counter based on the digit’s category.
- Repeat steps 4-5 for all digits in the number.
- Display the counts of even, odd, and zero digits.

Now that we have a clear understanding of the program’s objective and the steps involved, let’s dive into the code implementation:

Also Read: C Program to Copy the Contents of One File into Another File

```
#include <stdio.h>
int main() {
int number, digit, evenCount = 0, oddCount = 0, zeroCount = 0;
printf("Enter a number: ");
scanf("%d", &number);
while (number != 0) {
digit = number % 10;
if (digit == 0) {
zeroCount++;
} else if (digit % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
number /= 10;
}
printf("Even digits: %d\n", evenCount);
printf("Odd digits: %d\n", oddCount);
printf("Zero digits: %d\n", zeroCount);
return 0;
}
```

**Explaining the C Program**

Let’s break down the code to better understand its functionality:

Also Read: Getchar and Putchar Function in C with Example

```
#include <stdio.h>
int main() {
int number, digit, evenCount = 0, oddCount = 0, zeroCount = 0;
printf("Enter a number: ");
scanf("%d", &number);
```

In this section, we include the necessary header file `stdio.h`

and declare variables to store the input number, individual digits, and counters for even, odd, and zero digits.

Also Read: Best 5 Programs on Fibonacci Series in C

We prompt the user to enter a number and read it using `scanf`

.

```
while (number != 0) {
digit = number % 10;
if (digit == 0) {
zeroCount++;
} else if (digit % 2 == 0) {
evenCount++;
} else {
oddCount++;
}
number /= 10;
}
```

The core logic lies within this `while`

loop. We extract each digit of the number from right to left using the modulus operator `%`

with a base of 10.

Also Read: Program To Reverse a String in C using Pointer

If the digit is zero, we increment the `zeroCount`

variable. If the digit is even, we increment the `evenCount`

variable.

Otherwise, for odd digits, we increment the `oddCount`

variable.

Finally, we divide the number by 10 to remove the rightmost digit and continue the process until the number becomes zero.

Also Read: Find the Runner Up Score | Hackerrank Solution

```
printf("Even digits: %d\n", evenCount);
printf("Odd digits: %d\n", oddCount);
printf("Zero digits: %d\n", zeroCount);
return 0;
}
```

After counting the digits, we display the counts of even, odd, and zero digits using `printf`

. The program concludes by returning 0, indicating successful execution.

Also Read: 25 Tricky Questions on Pointers in C: Explained and Answered

**FAQs**

**Q: What is the purpose of counting even, odd, and zero digits in a number?**

The purpose of counting even, odd, and zero digits in a number is to gain insights into the distribution and characteristics of the number. It can be useful in various applications, such as data analysis, pattern recognition, and statistical calculations.

**Q: Can this program handle negative numbers?**

No, this program assumes the input number is non-negative. To handle negative numbers, additional logic can be implemented to consider the absolute value of the input.

**Q: Is there a limit to the size of the input number?**

The size of the input number is limited by the range of the `int`

data type in C. On most systems, this ranges from -2147483648 to 2147483647.

**Q: How does the program determine if a digit is even or odd?**

The program checks if a digit is even by verifying if it is divisible by 2 without leaving a remainder. If the digit satisfies this condition, it is considered even; otherwise, it is considered odd.

**Q: Can I use this program to count digits in a decimal number?**

No, this program is designed to count digits in a whole number. If you want to count digits in a decimal number, you would need to modify the program accordingly.

**Q: Is it possible to modify the program to count other digit categories?**

Certainly! By extending the program’s logic, you can count additional digit categories such as prime numbers, multiples of a specific digit, or any other desired classification.

Also Read: C Program to Remove Comments and White Spaces from a File

**Conclusion**

In this article, we explored a C program that counts even, odd, and zero digits in a given number.

By understanding the problem statement, logic, and code implementation, you have acquired valuable knowledge to solve similar challenges in your programming journey.

Also Read: Two Sum in C Programming with Solution

Remember to leverage the power of logic, iteration, and conditional statements to manipulate numbers effectively.

Keep practicing and exploring new programming concepts to further enhance your skills. Happy coding!

**Also Read: **Switch Case in C Program to Calculate Area of Circle and Triangle