1. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
The indentation is all over the place, and this may be not be helping.

Java Code:
```public class Premier
{
public static void main (String [] args)
{
System.out.print("Exercice 3\npartie 1\n");       // Partie 1

int nbdiviseurs = 0;
boolean premier = true;

for(int valeur = 1; valeur <= 1000; valeur++)
{
premier = true;
nbdiviseurs = 0;

for(int diviseur = 2; diviseur < valeur; diviseur++)
{
// inside this loop nbdiviseurs means "the number of divisors found
// so far"
if(valeur % diviseur == 0)
{
nbdiviseurs++;
premier = false;

if(nbdiviseurs == 1) // ie this is the *first* divisor
{
System.out.print(valeur+" est divisible par : ");
}

System.out.print(diviseur+","); // this is printed for *all* divisors
}

}
// now nbdiviseurs means "the *total* number of (proper) divisors"
if(nbdiviseurs == 0) // 0 means we have a prime
{
System.out.println(valeur+"  est premier");
}

if(nbdiviseurs > 1) // >1 means we have a composite
{
System.out.print(" soit "+nbdiviseurs+" diviseurs\n");
}

if(nbdiviseurs == 1) // 1 means we have the square of a prime
{
System.out.print(" soit "+nbdiviseurs+" diviseur\n");
}
}
}
}```

The reason why "valeur est divisible par" prints only once per valeur is that the "if(nbdiviseurs == 1)" means that the message is only printed for the *first* diviseur. Inside the diviseur loop the if statement means "if this is the first divisor that we have seen"

Outside (after) the loop the if statement looks similar but it has a different meaning: something like "if the total number of divisors is 1"

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12
I meant to add: well done in avoiding the List if you haven't come across these yet.

3. Member
Join Date
Oct 2010
Posts
16
Rep Power
0
OMG thks a lot...it makes much more sense now...thank you for you patience

4. Member
Join Date
Oct 2010
Posts
16
Rep Power
0
Also you explain really well

5. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,565
Rep Power
12

Page 2 of 2 First 12

#### Posting Permissions

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