I have the code to print prime numbers but I can't figure out how to print the numbers 8 to a row. This is because I have to find all the prime numbers <200.

import java.util.Scanner;

public class PrimeNumbers
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );
		
		//initialize variables
		int Number, Temp=1, Prime=3;
		
		System.out.print("Enter the number of prime numbers to display: ");
		Number=input.nextInt();
		
		if (Number>=1)
		{
			System.out.println("The first"+Number+" prime numbers are: ");
			System.out.println(2);
		}
		
		for (int count=2;count<=Number;)
		{
			for (int x=2;x<=Math.sqrt(Prime);x++)
			{
				if(Prime%x==0)
				{
					Temp=0;
					break;
				}
			}
			if (Temp!=0)
			{
				System.out.println(Prime);
				count++;
			}
			Temp=1;
			Prime++;
		}
	}

}

If you use the print() method without a newline character ('\n') the output will go on the same line. If you want future output to go on the next line use either the println() method or add a newline character: '\n' where you want the end of the current line to be.
If you are outputting the data as separate words, you need to count how many are on the current line and when you get to enough, print out a new line character.

If you use the print() method without a newline character ('\n') the output will go on the same line. If you want future output to go on the next line use either the println() method or add a newline character: '\n' where you want the end of the current line to be.
If you are outputting the data as separate words, you need to count how many are on the current line and when you get to enough, print out a new line character.

I'm looking to do something along the lines of:
2, 3, 5, 7, 11, 13, 17, 19
23, 29, etc up to 200 prime numbers

I tried

System.out.print(Prime)

It just give me the number all in a row with no space or anything

import java.util.Scanner;

public class PrimeNumbers
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );
		
		//initialize variables
		int Number, Temp=1, Prime=3;
		
		System.out.print("Enter the number of prime numbers to display: ");
		Number=input.nextInt();
		
		if (Number>=1)
		{
			System.out.println("The first "+Number+" prime numbers are: ");
			System.out.print("2");	//start prime numbers at 2
		}
		
		for (int count=2;count<=Number;)
		{
			for (int x=2;x<=Math.sqrt(Prime);x++)
			{
				if(Prime %x==0)
				{
					Temp=0;
					break;
				}
			}
			if (Temp!=0)
			{
				System.out.print(" , "+Prime);
				count++;
			}
			Temp=1;
			Prime++;
		}
	}
}

I've messed with and i get the answer to say 10 prime numbers as: 2,3,5,7,11,13,17,19,23,29

I'm trying to get (8 in a row):
2,3,5,7,11,19,17,19
23,29

I'll give some steps (you are really going in the right direction):
- loop from 2 to the user-entered number
- within the loop, loop from the current number to the square root (what you are doing)
- if the number is a prime:
- print the prime using System.out.print(prime).
- add 1 to a variable
- add a check if that variable is 7, if it is print a \n.

I'll give some steps (you are really going in the right direction):
- loop from 2 to the user-entered number
- within the loop, loop from the current number to the square root (what you are doing)
- if the number is a prime:
- print the prime using System.out.print(prime).
- add 1 to a variable
- add a check if that variable is 7, if it is print a \n.

So you're saying to declare another variable say:
int x=0;
x=x+1;
if(x=7)
{System.out.println(prime);}
//end

do this outside my loops? (I tried what I just asked and numbers went missing)

write a method that determines whether a number is a prime number or not
set counter to 0
set number to 0
loop while counter < 8
{
number = number + 1;
determine if number is a prime
if ( true )
{
print number
add 1 to counter
}
}

import java.util.Scanner;

public class PrimeNumbers
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );
		//initialize variables
		int Number, Temp=1, Prime=3;
		
		System.out.print("Enter the number of prime numbers to display: ");
		Number=input.nextInt();
		
		if (Number>=1)
		{
			System.out.println("The first "+Number+" prime numbers are: ");
			System.out.print("2");	//start prime numbers at 2
		}
		
		for (int count=2;count<=Number;)
		{
			for (int x=2;x<=Math.sqrt(Prime);x++)
			{
				if(Prime %x==0)	//if no prime then end
				{
					Temp=0;
					break;
				}
			}
			if (Temp!=0)	//if not zero and has prime then display
			{
				System.out.printf(" "+Prime);
				count++;
			}
			Temp=1;
			Prime++;
			
			if((count%9)==0)
			{
				System.out.println("");
			}
		}
	}
}

I can now get the numbers to print 8 to a row but the spacing is all messed up. I'll have 8 numbers then like 3 rows of blank a row of numbers, row of numbers, row of numbers, 7 blank rows, and numbers

the spacing is all messed up

Please post the programs output.
A picture is worth one thousand words.

What are you using to control when to output a newline character or call println()?

Please post the programs output.
A picture is worth one thousand words.

What are you using to control when to output a newline character or call println()?

Output:
1,2,3,4,5,6,7,8

9,10,11,12,13,14,15,16
17,18,19,20,21,22,23,24
25,26,27,28,29,30,31,32


33,34,35,36,37,38,39,40
//end it does something like that

It looks like you are printing out the loop control numbers not the prime numbers.

What are you using to control when to output a newline character or call println()?

It looks like you are printing out the loop control numbers not the prime numbers.

What are you using to control when to output a newline character or call println()?

import java.util.Scanner;

public class PrimeNumbers
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner( System.in );
		//initialize variables
		int Number, Temp=1, Prime=2;
		
		System.out.print("Enter the number of prime numbers to display: ");
		Number=input.nextInt();
		
		System.out.println("The first "+Number+" prime numbers are: ");
		//System.out.print("2,");
		
		for (int count=1;count<=Number;)
		{
			for (int x=2;x<=Math.sqrt(Prime);x++)
			{
				if(Prime %x==0)
				{
					Temp=0;
					break;
				}
			}
			if (Temp!=0)	//if not zero then display
			{
				System.out.print(""+Prime+",");
				count++;
			}
			Temp=1;
			Prime++;
			
			if(count%9==0)
			{
				System.out.println("");
			}
		}
	}
}

println("");

I don't understand why you have posted this code.
Have you fixed the problem?


I see the println() at the end of the post. Yes, that will go to a new line. That was not my question.
I asked: When do you decide to print out a new line?
What values do you test to decide NOW I WANT A NEW LINE?

What happens if you find a composite while count is a multiple of 9? You'll hit line 38, and that condition is true, so it prints a new line.

I don't understand why you have posted this code.
Have you fixed the problem?


I see the println() at the end of the post. Yes, that will go to a new line. That was not my question.
I asked: When do you decide to print out a new line?
What values do you test to decide NOW I WANT A NEW LINE?

I got it figured out. I had to include when to print a new line in the loop not outside of it.

now my next half of the problem is how to get it to display how many prime numbers are between 2 to user input

if((count%9)==0)

We gave very different instructions:

if (Prime%8== 0)

Moreover, make a method, that checks if the number is prime. I'll give some tips later today...

First of all, learn Java naming conventions:
- variables start with a small letter, and are connected with capital letters:

myNameIsThis

- Classes start with capital letters:

MyClassName

This is why the name of Number is not a good name. Number is also a Java Class, so you could mix up the two.
I changed your code a bit, see what you can do with it:

import java.util.Scanner;

public class PrimeNumbers {
	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);
		int num, printed = 0, currPrime = 1;

		System.out.print("Enter the number of prime numbers to display: ");
		num = input.nextInt();

		System.out.println("The first " + num + " prime numbers are: ");

		do {
			if (isPrime(currPrime)) {
				System.out.print(currPrime + "\t");
				printed++;
				if (printed % 8 == 0)
					System.out.println();
			}
			currPrime++;
		} while (printed < num);

		System.out.println();
	}
	private static boolean isPrime(int n) {
		for (int i = 2; i <= Math.sqrt(n); i += 2) {
			if (n % i == 0)
				return false;
			if (i == 2)
				i -= 1;
		}
		return true;
	}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.