# for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong

• 10-30-2011, 01:51 PM
for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
for(d=1;d<1000;d++)for(c=2;c<d;c++)d%c!=0;
what is wrong
Write an application that prints all prime numbers between 2 and 1000.that question is my hw
I use "for" wrongly but I couldnt find
it gaves eror that is "not a statement"
• 10-30-2011, 01:59 PM
Fubarable
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
For one thing you are not using whitespace appropriately. Place statements on their own lines, indent appropriately, and use curly brackets {} for all blocks including one line for loop blocks.
• 10-30-2011, 02:13 PM
JosAH
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
d%c != 0 isn't a statement; the compiler is right (as always).

kind regards,

Jos
• 10-30-2011, 02:46 PM
christopherx
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
I can see your thought process mate. Your code is, frankly, poorly structured but your reasoning is alright.

You've basically said that if a number, x, is not a multiple of any value between x - 1 and 0, then it must be a prime.
The problem is the code you've written doesn't reflect that thought path. and ! is not the mathematical symbol for factorial in Java, it is the logical symbol for NOT. On a side note, even if it was the mathematical symbol for factorial this doesn't work. You want x-1, x-2, x-3.. not (x * x - 1), (x * x - 2).. What you need too do, is loop through and test if, for each value of c, in decrements of 1, are multiples of d. If you get to 0 and no multiple has been found, then you have a prime. by the way, there are FAR more efficient ways of testing for primes, although they never yield mathematical certainty like this does :)
• 10-30-2011, 03:06 PM
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
thanks alot
• 10-30-2011, 03:46 PM
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
for(d=1;d<1000;d++)for(c=1;c<=d;c++){if(d%c==0){e+ +;if(e==2)System.out.println(d+"");}}
why it isnt correct it should be correct but it gaves just 2 as an answer
• 10-30-2011, 03:55 PM
JosAH
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
Why do you think it should be correct? I can see quite a few funny things going on there that most certainly can never be correct: e.g. what is that test for e == 2 doing? For a next pass through the inner loop, e might never be equal to 2 anymore ... oh, and b.t.w. you don't really indent your code like that, do you?

Jos
• 10-30-2011, 04:24 PM