# QuestionPractice problem help

#### Cece

##### New member
I am a beginner at C# and I have a practice problem that I cannot figure out. This is what I need to do:

• The program allows the users to specify how many random numbers (ranging from 1 to 100) to generate.
• The program generates the specified number random numbers.
• The program places an odd number in the oddNumListBox, and an even number to the evenNumListBox.
• The program calculates how many odd numbers and how many even numbers, and then display the counts respectively in the countOddNumLabel and countEvenNumLabel controls,
• You rare required to use at least one method in your code (in addition to even handlers).

This is the code I have so far:

```        private void generateButton_Click(object sender, EventArgs e)
{
try
{
int num; //ever or odd number
int count;
int countNumber = 0; //how many numbers

//generate random number between 1 and 100
Random rand = new Random();
rand.Next(1, 100);

for (count = 1; count <= 100; count++)
{
bool number = false;
if (num % 2)
{
countNumber = int.Parse(oddNumListBox.Text);
}
else
{
countNumber = int.Parse(evenNumListBox.Text);
}
}
}
catch
{
MessageBox.Show("Enter a valid number");
}
}
```

I am getting the exception regardless of what I put in. Please advice. Any help is appreciated. Thanks in advance!

#### Attachments

• OddEvenNumber.zip
18.2 KB · Views: 44
Last edited by a moderator:

#### jmcilhinney

##### C# Forum Moderator
Staff member
The first thing to do is not just use `catch` on its own. Get a reference to the exception and then you can actually look at it and use the information it provides to diagnose the issue. Do this instead:
`catch (Exception ex)`
and then you can use `ex` inside the `catch` block. You can determine much simply by the type of the exception, but you also have the error message, which tells you what happened, and the stack trace, which tells you where it happened.

You might also consider whether your code actually makes sense.
The program allows the users to specify how many random numbers (ranging from 1 to 100) to generate.
Where are you doing that? Surely you should have done that before anything else. What's the point of going on to step 2 if you haven't actually implemented step 1? Nowhere in that code are you taking input from the user.
The program generates the specified number random numbers.
How many random numbers is your code going to generate? Are you doing anything useful with those random numbers?
The program calculates how many odd numbers and how many even numbers, and then display the counts respectively in the countOddNumLabel and countEvenNumLabel controls
Where are you actually counting anything? You're assigning a value to `count` but what is that actually a count of?
You rare required to use at least one method in your code (in addition to even handlers)
Not seeing any methods in your code other than the event handler.

#### Cece

##### New member

Can you please give me an example of how to prompt the user?

The user will select the amount of random number between 1 and 100 and they should be listed at the end.

As far as the count, once the user puts in how many random numbers to generate, I need to count how many are even and how many odd and add them to the correct list box.

I work well off examples so I was attempting to mirror a couple examples I found but it is not coming together. If you can show me some code examples, that would help a lot! Thanks for all your assistance so far!

#### jmcilhinney

##### C# Forum Moderator
Staff member
As far as the count, once the user puts in how many random numbers to generate, I need to count how many are even and how many odd and add them to the correct list box.

No, read what you actually wrote. You need to put them in the ListBoxes and THEN you need to count them. Not the other way around. I'm fairly sure that you can find an example of counting the number of items in a ListBox.

#### Cece

##### New member
Ok...:emptiness:...but thanks anyways...

#### jmcilhinney

##### C# Forum Moderator
Staff member
No, read what you actually wrote. You need to put them in the ListBoxes and THEN you need to count them. Not the other way around. I'm fairly sure that you can find an example of counting the number of items in a ListBox.

Alternatively, just count them as you go. How do you usually count things? You start with the number zero and then you add 1 to it for each item you count. Why would it be any different in programming?

#### eduardovm

##### New member
First, create a TextBox in your form to get how much numbers to generate.

Change the name of the TextBox to txtBoxHowMany.

And use this code on the "Click event" of the button

```// Variable to count even
int countEven = 0;

// Variable to count odd
int countOdd = 0;

// Variable to get how many numbers to generate
int howMany;

// Variable to store the random number
int num;

// Get how many numbers to generate
howMany = int.Parse(txtBoxHowMany.Text);

//generate random object
Random rand = new Random();

// Do a for 0 to howMany, especified by the user
for (int count = 0; count <= howMany; count++)
{

// Generate number between 1 and 100
num = rand.Next(1, 100);

// Verify if is even or odd
if (num % 2 == 0)
{

// Increment count of the even numbers
countEven++;

// Put the number in the list
}
else
{
// Incremente count of the odd numbers
countOdd++;

// Put the number in the list
}

}
```

Now, you have the two variables countEven and countOdd, with the total of even numbers and odd numbers respectively

Replies
8
Views
309
Replies
4
Views
340
Replies
2
Views
169
Replies
1
Views
659
Replies
2
Views
136