I wrote a simple C# console application that gets all prime numbers, and stacks them into a string variable.
My question is why does the first "if" accept all the numbers? I think the rule I wrote is fine. My code (of course all the variables are formatted in the head of the script):
check++;
number++;
num = number;
if (number / num == 1 && number / 1 == num)
{
Console.WriteLine("Working...");
Console.WriteLine("Numbers Checked: " + check);
produce++;
Console.WriteLine("Numbers Prodused: " + produce);
Console.WriteLine("Numbers Failed : " + fail);
System.Threading.Thread.Sleep(1);
Console.Clear();
//storing the numbers
str += Convert.ToString(number);
}
else
{
fail++;
}
Console.WriteLine(str);
The problem is that when I run the program, the fail indicator stays on 0 which means the loop is processing all the numbers with no exception for the prime numbers.
How do I fix that? Is my "if (rule)" wrong?
EDIT ! :
i started fresh ;) here's what i did:
bool isPrime = false;
int number = 0, counter = 0;
while (number <= counter)
{
counter++;
number++;
isPrime = true;
for (int i = 2; i < number; ++i)
{
if (number % i == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
System.Threading.Thread.Sleep(1);
Console.WriteLine(number);
Console.Beep();
}
Console.WriteLine("Try Number " + counter);
works great, I'll continue developing it into a windows forms application soon ;)