Results 1 to 9 of 9
 10302011, 01:51 PM #1Member
 Join Date
 Oct 2011
 Posts
 24
 Rep Power
 0
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"Last edited by jihad; 10302011 at 01:54 PM.

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.
Last edited by Fubarable; 10302011 at 02:28 PM.
 10302011, 02:13 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,016
 Blog Entries
 7
 Rep Power
 23
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,
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 10302011, 02:46 PM #4Member
 Join Date
 Oct 2011
 Posts
 92
 Rep Power
 0
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 x1, x2, x3.. 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 :)
 10302011, 03:06 PM #5Member
 Join Date
 Oct 2011
 Posts
 24
 Rep Power
 0
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
thanks alot
 10302011, 03:46 PM #6Member
 Join Date
 Oct 2011
 Posts
 24
 Rep Power
 0
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
 10302011, 03:55 PM #7
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,016
 Blog Entries
 7
 Rep Power
 23
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?
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 10302011, 04:24 PM #8Member
 Join Date
 Oct 2011
 Posts
 24
 Rep Power
 0
Re: for(d=1;d<1000;d++)for(c=1;c<d;c++)d%c!=0; what is wrong
if e has 2 value it means the number is primer
 10302011, 04:27 PM #9Member
 Join Date
 Oct 2011
 Posts
 24
 Rep Power
 0
Similar Threads

Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06112013, 01:37 AM 
Win 1000$ Now [Java WebApp]
By biz in forum Java SoftwareReplies: 1Last Post: 10012011, 07:17 PM 
Perfect number 11000 program help
By ImWithStupid in forum New To JavaReplies: 9Last Post: 04282011, 08:22 AM 
approx. 1000 instances of class?
By artemff in forum CLDC and MIDPReplies: 0Last Post: 01012010, 08:57 PM
Bookmarks