Search code examples
c#integer

How to write calculation correctly for program that checks what numbers between 1 and 1000 are dividable by entered value ( int variables)


I am writing a program that first lets a user enter a number between 1 and 10, and then checks what numbers between 1 and 1000 are dividable by that number. both variables used are int variables, so for example if a user enters 4, it should read: 1 8 12 16 etc.

thusfar i have the following code:


        private void button1_Click(object sender, EventArgs e)
        {
            int Getal1 = 1;
            int Getal2;

            Getal2 = int.Parse(textBox1.Text);//textbox1 to getal2

            for (Getal1 = 1; Getal1 <= 1000; Getal1++)//// for loop.
            {
                textBox2.Text = textBox2.Text + "\r\n" + Getal1 / Getal2 +  "\r\n";
            }
        }

the textbook mentions using the following code : Getal1 % Getal2.. i tried adding it after the calculation above, but this didnt work.. math is not my strong point at all.. does someone have an explanation on this? maybe if Getal1 % Getal2 = 0??.. im really guessing here..

thanks for any help in advance,

Stefan

I tried adding code from the textbook, expecting the program to work correctly, right now it does give a result, but not a correct result.. for example if i enter 2 it starts the sequence with the numbers 123..


Solution

  • for (Getal1 = 1; Getal1 <= 1000; Getal1++)//// for loop.
            {
                if (Getal1 % Getal2 == 0)
                {
                    textBox2.Text += Getal1.ToString() + Environment.NewLine;
                }
            }