+ 1
Coding help! C# VS
So, I'm trying to make a factor machine, which I have all the code expect I don't know how to find the factor. So you could pick 6, 1, 2, 3, 6. Here's what I think the code could be. int x = Microsoft.VisualBasic.Interaction.InputBox("Factor", "yes"); int a; for (int i = 0; i < x; i++;) { if (x % 2 == x ) { a = x; lstData.Items.Add("Factors: " + a); } }
3 Réponses
+ 1
If I understood you correctly, your loop should've been like this instead;
for (int i = 2; i < x; i++)
{
if (x % i == 0)
{
lstData.Items.Add("Factors: " + i);
}
}
Explanation:
I initialized i with 2 in the for loop because we know a number divided by 1 leaves no remainder, thus we can skip 1 altogether, and dividing by zero is totally prohibited, modulo internally do division and returns the remainder of the division operation (cmiiw).
Hth, cmiiw
+ 1
thanks
+ 1
You're welcome : )