Results 1 to 2 of 2
  1. #1
    eng_hyzoom is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default displaying prime number

    what is error in this code
    ــــــــــــــــــــــــــــــــــــــــــــــــــ ــــــــــــــــــــــــــــــــــــــــــــــــــ ــــــــــــــــــــــــــــــ

    public class Exercise9_6 {
    public static void main(String[] args) {
    final int LIMIT = 120;
    int count = 0;
    StackOfIntegers stack = new StackOfIntegers();

    // Repeatedly find prime numbers
    for (int number = 2; number < LIMIT; number++)
    if (isPrime(number)) {
    stack.push(number);
    count++; // Increase the prime number count
    }

    // Print the first 30 prime numbers in decreasing order
    System.out.println("The prime numbers less than 120 are \n");
    final int NUMBER_PER_LINE = 10;

    while (!stack.empty()) {
    System.out.print(stack.pop() + " ");

    if (stack.getSize() % NUMBER_PER_LINE == 0)
    System.out.println(); // advance to the new line
    }
    }

    public static boolean isPrime(int number) {
    // Assume the number is prime
    boolean isPrime = true;

    // Exercise3_21 if number is prime
    for (int divisor = 2; divisor <= number / 2; divisor++) {
    //If true, the number is not prime
    if (number % divisor == 0) {
    // Set isPrime to false, if the number is not prime
    isPrime = false;
    break; // Exit the for loop
    }
    }

    return isPrime;
    }
    }
    ــــــــــــــــــــــــــــــــــــــــــــــــــ ــــــــــــــــــــــــــــــــــــــــــــــــــ ــــــــــــــــــــــــــــــ

    thanks

  2. #2
    Join Date
    Jul 2010
    Location
    India
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by eng_hyzoom View Post
    what is error in this code
    ــــــــــــــــــــــــــــــــــــــــــــــــــ ــــــــــــــــــــــــــــــــــــــــــــــــــ ــــــــــــــــــــــــــــــ
    Explain the exact error you are facing.Try to be specific as much as possible and use tag for your codes.
    _______________________________________________
    give me beans .........

Similar Threads

  1. Prime Number - true , false
    By pinkdreammsss in forum Java Applets
    Replies: 11
    Last Post: 05-04-2010, 02:49 PM
  2. Finding nth prime number
    By dextr in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 11:42 PM
  3. Replies: 10
    Last Post: 02-20-2010, 08:25 PM
  4. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 AM

Posting Permissions

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