Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: help with a for loop

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with a for loop

    I have this assignment to do a loop to determine all the prime numbers from 1 to 1000, but to try it, i did my loop from 1 to 10.

    This is my code:

    public class premier
    {
    public static void main (String [] args)
    {
    int diviseur = 0;
    int resultat = 0;
    int nombre = 0;
    int diviseurs = 0;
    int nbdiviseurs = 0;

    for(int valeur = 1; valeur <= 10; valeur++)
    {
    for(diviseur = 2; diviseur < valeur; diviseur++)
    {
    resultat = valeur % diviseur;

    if(resultat != 0)
    {
    System.out.println(valeur+" est premier");
    }

    else
    {

    diviseurs = diviseur;
    System.out.println(valeur+ " est divisible par : "+diviseurs);
    }
    }
    }
    }
    }

    However, because my system.out.print is i the diviseur loop. it prints for every diviseur

    Print:

    3 est premier
    4 est divisible par : 2
    4 est premier
    5 est premier
    5 est premier
    5 est premier
    6 est est divisible par : 2
    6 est divisible par : 3
    6 est premier
    6 est premier
    ...

    However, I am supposed to do

    3 est premier
    4 est divisible par : 2, soit 1 diviseurs
    5 est premier
    6 est divisible par 2, 3, soit 2 diviseurs
    ...

    However everytime I try printing out of the diviseur loop, the modulo is not the right value anymore so it prints est premier for all values.

    thks a lot


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with a for loop

    Hi,

    here is a simple code that displays prime number from 1-1000. You can change the UPPER_LIMIT to any number. Since I don't speak french (I think it is french?), your code is a bit confusing for me. This code is doesn't use advanced java coding. I hope you understand the drift of it.
    Hope it helps

    here is the code:


    public class Main {

    private final int UPPER_LIMIT = 1000;

    public void calculatePrimeNumbers() {

    int i = 0;
    int primeNumberCounter = 0;

    while (++i <= UPPER_LIMIT) {

    int i1 = (int) Math.ceil(Math.sqrt(i));

    boolean isPrimeNumber = false;

    while (i1 > 1) {

    if ((i != i1) && (i % i1 == 0)) {
    isPrimeNumber = false;
    break;
    } else if (!isPrimeNumber) {
    isPrimeNumber = true;
    }

    --i1;
    }

    if (isPrimeNumber) {
    System.out.println(i);
    ++primeNumberCounter;
    }
    }

    System.out.println("Nr of prime numbers found: " + primeNumberCounter);
    }

    public static void main(String[] args) {
    new Main().calculatePrimeNumbers();
    }
    }

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: help with a for loop

    Cross posted
    for loop - Java Forums

    db

Similar Threads

  1. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  2. Help with my loop
    By Xrrak in forum Loops & Control Statements
    Replies: 2
    Last Post: April 14th, 2010, 08:14 AM
  3. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM
  4. Need help with loop
    By SwEeTAcTioN in forum Loops & Control Statements
    Replies: 8
    Last Post: October 25th, 2009, 05:59 PM
  5. can any one do this in a loop? (for loop)
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: October 4th, 2009, 08:31 PM