# odd and even numbers counter

### Question

• In this code I need to show how many odd numbers and how many even numbers there

for example you accept a number and you enter the following numbers 123456

so the program should gives you there are 3 even and 3 odd numbers:

In this code I need to show how many odd numbers and how many even numbers there. so in this code it doesnt show that operation but the elements in that number which us is six number.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication18
{
class Program
{
int n, t;
int o = 0;
int e = 0;
public void dis()
{

Console.WriteLine("Enter the number to count");
while(n>0)
{
t = n % 10;
if (t == 0)
{
e++;
}
else
{
o++;
}
n = n / 10;
}
Console.WriteLine("The even number count is:" + e);
Console.WriteLine("The odd number count is:" + o);
}
class second
{
static void Main(string[] args)
{
Program p = new Program();
p.dis();
}
}
}
}

prog

Monday, June 04, 2012 10:17 AM

• When you guys give full code solutions to academic problems, especially with absolutely no explanations, you are not helping, and you are often being harmful.

@msdhhhh

First off, you shouldn't be using single letter variables like that; it makes your code much harder to read.  Use full words, or even short phrases (when appropriate) for your variable names.  For example, 'e' should be something like 'evenCount', and 'o' should be something like 'oddCount'.  Intellisense will help you with it's code completion so that you don't need to type more than a few letters per variable each time; it really doesn't slow you down much at all.

Since all four of your instance variables aren't used outside of a single method, they really should be local variables in that one method.  As a general rule, always have your variables at the smallest scope they can be while still being accessible everywhere that you need/want them to be accessed.

In general, you're really, really close to solving the problem with the code that you have, you've just missed one small step.  In your code, 't' is one digit of the user entered input.  Your code says, "if 't' is equal to zero, then it's even".  That's not quite what you want.  If 't' mod two ("t % 2") is equal to zero, then it's even.  If you just add in those two extra characters your code should work properly.

One other issue that you have is that your code will ignore all leading zeros.  If a user enters "0001" your code will say that there is 1 odd and 0 even numbers, but if they enter "1000" it will say that there is 1 odd and 3 even numbers.  If that's what you want, then great.  If it's not, then you'll need to modify your 'while' loop slightly.  You'll want to store the string the user enters before converting it into an int (meaning adding a new variable) and having a for loop from 0 to the length of the string (the code inside the loop need not change at all).

Monday, June 04, 2012 1:54 PM
• Hi,

The the code below, do not make it complex, I dont understand why you do n=n/10????

static void Main()
{
for (int i = 0; i <= 100; i++)
{
if (IsOdd(i))
{
Console.WriteLine(i);
}
}
}

public static bool IsOdd(int value)
{
return value % 2 != 0;
}

Thanks
Md. Marufuzzaman

Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

Monday, June 04, 2012 10:36 AM
• If you want to count the even and odd digits in a number, then try this too:

int even = number.Count( d => "02468".Contains( d ) );
int odd = number.Length - even;

Console.WriteLine( "Number {0} contains {1} even and {2} odd digits.", number, even, odd );

Monday, June 04, 2012 11:37 AM

### All replies

• Hi,

The the code below, do not make it complex, I dont understand why you do n=n/10????

static void Main()
{
for (int i = 0; i <= 100; i++)
{
if (IsOdd(i))
{
Console.WriteLine(i);
}
}
}

public static bool IsOdd(int value)
{
return value % 2 != 0;
}

Thanks
Md. Marufuzzaman

Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.

Monday, June 04, 2012 10:36 AM
• Try it this way:

static void Main(string[] args)
{
Console.WriteLine("Enter a number:");
int[] listNums = num.ToString().ToCharArray().Select(s => int.Parse(s.ToString())).ToArray();
int even = 0;
int odd = 0;
for (int i = 0; i < listNums.Length; i++)
{
//listNums[i] % 2 == 0 ? even++ : odd++;
if (listNums[i] % 2 == 0)
even++;
else
odd++;

}
Console.WriteLine("The even number count is {0}\r\nThe odd number count is {1}.", even, odd);
}

Mitja

Monday, June 04, 2012 10:42 AM
• If you want to count the even and odd digits in a number, then try this too:

int even = number.Count( d => "02468".Contains( d ) );
int odd = number.Length - even;

Console.WriteLine( "Number {0} contains {1} even and {2} odd digits.", number, even, odd );

Monday, June 04, 2012 11:37 AM
• When you guys give full code solutions to academic problems, especially with absolutely no explanations, you are not helping, and you are often being harmful.

@msdhhhh

First off, you shouldn't be using single letter variables like that; it makes your code much harder to read.  Use full words, or even short phrases (when appropriate) for your variable names.  For example, 'e' should be something like 'evenCount', and 'o' should be something like 'oddCount'.  Intellisense will help you with it's code completion so that you don't need to type more than a few letters per variable each time; it really doesn't slow you down much at all.

Since all four of your instance variables aren't used outside of a single method, they really should be local variables in that one method.  As a general rule, always have your variables at the smallest scope they can be while still being accessible everywhere that you need/want them to be accessed.

In general, you're really, really close to solving the problem with the code that you have, you've just missed one small step.  In your code, 't' is one digit of the user entered input.  Your code says, "if 't' is equal to zero, then it's even".  That's not quite what you want.  If 't' mod two ("t % 2") is equal to zero, then it's even.  If you just add in those two extra characters your code should work properly.

One other issue that you have is that your code will ignore all leading zeros.  If a user enters "0001" your code will say that there is 1 odd and 0 even numbers, but if they enter "1000" it will say that there is 1 odd and 3 even numbers.  If that's what you want, then great.  If it's not, then you'll need to modify your 'while' loop slightly.  You'll want to store the string the user enters before converting it into an int (meaning adding a new variable) and having a for loop from 0 to the length of the string (the code inside the loop need not change at all).

Monday, June 04, 2012 1:54 PM