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..
CodePudding user response:
for (Getal1 = 1; Getal1 <= 1000; Getal1 )//// for loop.
{
if (Getal1 % Getal2 == 0)
{
textBox2.Text = Getal1.ToString() Environment.NewLine;
}
}
CodePudding user response:
You can omit the division checking whatsoever by starting with the number introduced by the user and then adding this number in the loop. This way you will never get a number that is not dividable.
For example:
- if user inputs
4
, you start with4
- than you add
4
to it and get8
- add
4
again, get12
and so on
private void button1_Click(object sender, EventArgs e)
{
int Getal2 = int.Parse(textBox1.Text);//textbox1 to getal2
for (int Getal1 = Getal2; Getal1 <= 1000; Getal1 = Getal2)//// for loop.
{
textBox2.Text = Getal1 "\r\n";
}
}