import java.util.Scanner; public class VLV58750a10 { public static void main(String[] args) { final int NUMBER of PRIMES PER LINE = 10; //Displays only 10 prime numbers per line int x; int y; int userinput; boolean primenumber; Scanner kb = new Scanner(System.in); System.out.print("Enter a number: "); userinput = kb.nextInt(); //Finds prime numbers for (x = 2; x < userinput; x++) { primenumber = true; for (y = 2; primenumber && y < x; y++) { if (x % y == 0) primenumber = false; } if (primenumber || x==2) System.out.print(x + " "); } } }

## 8 Replies - 1477 Views - Last Post: 15 March 2012 - 10:33 PM

### #1

# change existing program to use isPrime(int number) method at least

Posted 14 March 2012 - 12:35 PM

This program determines the number of prime numbers less than N which is given by the user. I have to incorporate the method, isPrime(int number). I also would like to display only 10 prime numbers per line. Here is my coding so far:

##
**Replies To:** change existing program to use isPrime(int number) method at least

### #2

## Re: change existing program to use isPrime(int number) method at least

Posted 14 March 2012 - 09:38 PM

You can always intialized a int nbPrime = 0;

each time you print a prime number do

if(++nbPrime % 10 == 0) System.out.pritnln();

Also I suggest that you print 2 first than do

for(x = 3; x < userInput; x += 2)

that will avoid to test all the even numbers

Same thing for the loop that generates the y

each time you print a prime number do

if(++nbPrime % 10 == 0) System.out.pritnln();

Also I suggest that you print 2 first than do

for(x = 3; x < userInput; x += 2)

that will avoid to test all the even numbers

Same thing for the loop that generates the y

### #3

## Re: change existing program to use isPrime(int number) method at least

Posted 15 March 2012 - 08:27 PM

How can I get only 10 numbers per line?

### #4

## Re: change existing program to use isPrime(int number) method at least

Posted 15 March 2012 - 08:29 PM

Read the first 3 lines of my previous reply

### #5

## Re: change existing program to use isPrime(int number) method at least

Posted 15 March 2012 - 09:07 PM

Sorry! I forgot to mention that I put the code in. But when I ran the program and entered 200 as the integer, the output only gave me 4 numbers: 29, 71, 113, 173.

### #6

## Re: change existing program to use isPrime(int number) method at least

Posted 15 March 2012 - 09:20 PM

Nothing to do with your 10 numbers per line as you have only 4 of them

Post your updated code

Post your updated code

### #7

## Re: change existing program to use isPrime(int number) method at least

Posted 15 March 2012 - 09:26 PM

Good idea! I probably entered it wrong.

Sorry. I don't know why extra space is added when I post my coding sometimes.

import java.util.Scanner; public class VLV58750a10 { public static void main(String[] args) { int number; int divisor; int userinput; int nbPrime = 0; boolean primenumber; // will allow primenumber to be true or false Scanner kb = new Scanner(System.in); System.out.print("Enter a positive integer: "); userinput = kb.nextInt(); // allows user to input a number // Check and find prime numbers for (number = 2; number < userinput; number++) { primenumber = true; // number is prime for (divisor = 2; divisor <= number / 2; divisor++) { if (number % divisor == 0) // if true, number is not prime primenumber = false; // number is not a prime } if (primenumber || number == 2) // prints out only prime numbers if (++nbPrime % 10 == 0) System.out.println(number + " "); } } }

Sorry. I don't know why extra space is added when I post my coding sometimes.

### #8

## Re: change existing program to use isPrime(int number) method at least

Posted 15 March 2012 - 09:39 PM

Your indentation is horrible ! And I told you to use only odd numbers.

And no need to declare all your variables at the top of the method. Declare them only when you need them.

And no need to declare all your variables at the top of the method. Declare them only when you need them.

public static void main(String[] args) { int nbPrimePrinted = 1; // I will print 2 so it is one printed Scanner kb = new Scanner(System.in); System.out.print("Enter a positive integer: "); int userinput = kb.nextInt(); // allows user to input a number // print 2 the first one after that I will only check for odd number // Check and find prime numbers for (int number = 3; number < userinput; number += 2) { boolean primenumber = true; // number is prime // check only for even number no need to go further than sqrt(number) for (int divisor = 3; divisor <= Math.sqrt(number); divisor += 2) { if (number % divisor == 0) {// if true, number is not prime primenumber = false; // number is not a prime break; // not prime no need to change the other } } if (primenumber) { // prints out only prime numbers System.out.print(" " + number); if (++nbPrimePrinted % 10 == 0) System.out.println(); } } }

### #9

## Re: change existing program to use isPrime(int number) method at least

Posted 15 March 2012 - 10:33 PM

Haha sorry. My coding doesnt look like that. For some reason, when I copy and paste it here, it changes the spacing and indention. But yes, I agree with you about declaring variables only when you need them. I declared them at the beginning because that's how my professor wants it done. But thank you for your help! I will try that tomorrow!!

Page 1 of 1