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

1. Member
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; 10-30-2011 at 01:54 PM.

2. ## 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; 10-30-2011 at 02:28 PM.

3. ## 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

4. Member
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 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 :)

5. Member
Join Date
Oct 2011
Posts
24
Rep Power
0

thanks alot

6. Member
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

7. ## 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

8. Member
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

9. Member
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

yes you are right I didnt see :D it is really funny

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•