c# - count odd and even digits in a number

C# - count odd and even digits in a number

To count the number of odd and even digits in a given number in C#, you can follow these steps:

  1. Convert the Number to a String: This allows you to easily iterate through each digit.
  2. Iterate Through Each Digit: Convert each character back to an integer to determine if it's odd or even.
  3. Count the Digits: Use counters to keep track of the number of odd and even digits.

Here's a sample C# program to achieve this:

using System; class Program { static void Main() { Console.Write("Enter a number: "); string input = Console.ReadLine(); // Initialize counters int oddCount = 0; int evenCount = 0; // Iterate through each character in the string foreach (char c in input) { // Check if the character is a digit if (char.IsDigit(c)) { int digit = c - '0'; // Convert char to int // Check if the digit is odd or even if (digit % 2 == 0) { evenCount++; } else { oddCount++; } } } // Output the results Console.WriteLine($"Odd digits count: {oddCount}"); Console.WriteLine($"Even digits count: {evenCount}"); } } 

Explanation

  1. Input Handling:

    • The user is prompted to enter a number, which is read as a string.
  2. Initialization:

    • Two counters, oddCount and evenCount, are initialized to zero.
  3. Iteration:

    • The program iterates through each character in the string.
    • For each character, it checks if it is a digit using char.IsDigit(c).
    • The character is then converted to an integer using c - '0'.
  4. Odd and Even Check:

    • The digit is checked to determine if it's odd or even by using the modulus operator %.
  5. Output:

    • The counts of odd and even digits are displayed.

Edge Cases

  • Non-Digit Characters: Characters that are not digits are ignored.
  • Empty Input: If the input is empty, both counts will remain zero.
  • Negative Numbers: If the input is negative, the '-' sign is ignored as it's not a digit.

You can test this program by entering different numbers to see how it counts the odd and even digits.

Examples

  1. "c# - Count odd and even digits in a number using simple loop"

    • Description: Use a loop to iterate through each digit of the number, and count odd and even digits.
    • Code:
      using System; class Program { static void Main() { int number = 123456789; int oddCount = 0, evenCount = 0; while (number > 0) { int digit = number % 10; if (digit % 2 == 0) { evenCount++; } else { oddCount++; } number /= 10; } Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } } 
  2. "c# - Count odd and even digits using LINQ"

    • Description: Use LINQ to process the digits of a number and count odd and even digits.
    • Code:
      using System; using System.Linq; class Program { static void Main() { int number = 123456789; var digits = number.ToString().Select(c => int.Parse(c.ToString())); int oddCount = digits.Count(digit => digit % 2 != 0); int evenCount = digits.Count(digit => digit % 2 == 0); Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } } 
  3. "c# - Count odd and even digits using recursion"

    • Description: Use recursion to count odd and even digits in a number.
    • Code:
      using System; class Program { static void Main() { int number = 123456789; (int oddCount, int evenCount) = CountDigits(number); Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } static (int oddCount, int evenCount) CountDigits(int number) { if (number == 0) return (0, 0); int digit = number % 10; var (oddCount, evenCount) = CountDigits(number / 10); if (digit % 2 == 0) { evenCount++; } else { oddCount++; } return (oddCount, evenCount); } } 
  4. "c# - Count odd and even digits in a number using string manipulation"

    • Description: Convert the number to a string, then count odd and even digits.
    • Code:
      using System; class Program { static void Main() { int number = 123456789; string numStr = number.ToString(); int oddCount = 0, evenCount = 0; foreach (char c in numStr) { int digit = c - '0'; if (digit % 2 == 0) { evenCount++; } else { oddCount++; } } Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } } 
  5. "c# - Count odd and even digits using a helper method"

    • Description: Implement a helper method to count odd and even digits.
    • Code:
      using System; class Program { static void Main() { int number = 123456789; var (oddCount, evenCount) = CountOddEvenDigits(number); Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } static (int oddCount, int evenCount) CountOddEvenDigits(int number) { int oddCount = 0, evenCount = 0; while (number > 0) { int digit = number % 10; if (digit % 2 == 0) { evenCount++; } else { oddCount++; } number /= 10; } return (oddCount, evenCount); } } 
  6. "c# - Count odd and even digits with absolute value handling"

    • Description: Handle negative numbers by using the absolute value of the number.
    • Code:
      using System; class Program { static void Main() { int number = -123456789; number = Math.Abs(number); // Handle negative number int oddCount = 0, evenCount = 0; while (number > 0) { int digit = number % 10; if (digit % 2 == 0) { evenCount++; } else { oddCount++; } number /= 10; } Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } } 
  7. "c# - Count odd and even digits using a for loop"

    • Description: Use a for loop to iterate through the digits and count odd and even digits.
    • Code:
      using System; class Program { static void Main() { int number = 123456789; string numStr = number.ToString(); int oddCount = 0, evenCount = 0; for (int i = 0; i < numStr.Length; i++) { int digit = numStr[i] - '0'; if (digit % 2 == 0) { evenCount++; } else { oddCount++; } } Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } } 
  8. "c# - Count odd and even digits in a long number"

    • Description: Handle counting odd and even digits in a long data type.
    • Code:
      using System; class Program { static void Main() { long number = 9876543210; int oddCount = 0, evenCount = 0; while (number > 0) { int digit = (int)(number % 10); if (digit % 2 == 0) { evenCount++; } else { oddCount++; } number /= 10; } Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } } 
  9. "c# - Count odd and even digits with digit filtering"

    • Description: Filter out non-digit characters when counting odd and even digits.
    • Code:
      using System; class Program { static void Main() { string input = "1234abc5678"; int oddCount = 0, evenCount = 0; foreach (char c in input) { if (char.IsDigit(c)) { int digit = c - '0'; if (digit % 2 == 0) { evenCount++; } else { oddCount++; } } } Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } } 
  10. "c# - Count odd and even digits in a number with validation"

    • Description: Validate input and count odd and even digits in a valid integer.
    • Code:
      using System; class Program { static void Main() { string input = "123456789"; if (int.TryParse(input, out int number)) { int oddCount = 0, evenCount = 0; while (number > 0) { int digit = number % 10; if (digit % 2 == 0) { evenCount++; } else { oddCount++; } number /= 10; } Console.WriteLine($"Odd digits: {oddCount}, Even digits: {evenCount}"); } else { Console.WriteLine("Invalid number."); } } } 

More Tags

apache2.4 linear-gradients ntfs collision-detection mat-tab nan shiny-server highlight morse-code hide

More Programming Questions

More Auto Calculators

More Other animals Calculators

More Livestock Calculators

More Biology Calculators