I have to do a program in which I create two NumericUpDown controls and the user will input a minimum value in one and a maximum value in the other. Then, when the user hits the calculate button, it should show a Message Box containing all even numbers between the minimum and the maximum value and the sum of all of those even numbers.

My problem is that I know how to determine the even numbers, but I'm not sure how can I display them in a MessageBox. Can you guys give me a slight hint on how to do this?

Hmmm do you know how to build a string (or list of strings) out of these numbers?

private string GetEvenNums(int Min, int Max)
{
   string returnString = "";
   for (int i = Min; i <= Max; i++)
      if (i % 2 == 0)
          returnString += i.ToString() + Environment.Newline;
   return returnString;
}

//in the calling method:
MessageBox.Show("Even numbers: " + Environment.Newline + GetEvenNums(min, max))

If there's a lot of numbers I would seperate them with a comma instead of an Environment.Newline - otherwise you're message box will get too long (OK button will be off screen).

MessageBox.Show();

You can convert your numbers to one giant string to concatenate them into something that will show in a MessageBox.

or use string.format method:

private void DoCalculation(int min, int max)
{
    List<int> listOfEvenNumbers = new List<int>();
    for(int i = min; i <= max; i++)
    {
         if(i % 2 == 0)
              listOfEventNumbers.Add(i);
    }
    StringBuilder sb = new StringBuilder();
    foreach(int  number in listOfEvenNumbers)
         sb.Addend(number.ToString() + ", ");
    MessageBox.Show(String.Format("The result is:\r\n- Even numbers: {0}\r\n- Sum of numbers: {1}", sb.ToString(), listOfEvenNumbers.Sum()));
}

Hmmm do you know how to build a string (or list of strings) out of these numbers?

private string GetEvenNums(int Min, int Max)
{
   string returnString = "";
   for (int i = Min; i <= Max; i++)
      if (i % 2 == 0)
          returnString += i.ToString() + Environment.Newline;
   return returnString;
}

//in the calling method:
MessageBox.Show("Even numbers: " + Environment.Newline + GetEvenNums(min, max))

If there's a lot of numbers I would seperate them with a comma instead of an Environment.Newline - otherwise you're message box will get too long (OK button will be off screen).

Don't build strings from strings, that's what StringBuilder is for.

I'm adding this because no one actually answered his question, just gave him code.

We can take the basic principle that all even numbers are divisible by 2 and have no remainder.

So, what you need to know is how to calculate the remainder, or modulus of each number.

As you see in the code given to you above, calculating the modulus is as easy as using the % sign. This command calculates the division and returns the remainder to you.

4 % 2 = 0;
5 % 3 = 2;
120 % 47 = 26;

So for calculating an even number, you are checking if the modulus of the number divided by two is equal to zero. if( (number % 2) == 0 ) That's how the code given to you above is working =)

I'm adding this because no one actually answered his question, just gave him code.

We can take the basic principle that all even numbers are divisible by 2 and have no remainder.

So, what you need to know is how to calculate the remainder, or modulus of each number.

As you see in the code given to you above, calculating the modulus is as easy as using the % sign. This command calculates the division and returns the remainder to you.

4 % 2 = 0;
5 % 3 = 2;
120 % 47 = 26;

So for calculating an even number, you are checking if the modulus of the number divided by two is equal to zero. if( (number % 2) == 0 ) That's how the code given to you above is working =)

The OP said that he already knows how to get the even numbers, therefore your reply is less useful than us 'just posting code'.

@Momerath - string concatenation is actually the fastest way to build strings, I read an article about it a while ago with some pretty thorough testing (that tested stringbuilder, string.join, string.format, and string concatenation operators). If you want I can try and find the link.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.