How to print 10 prime numbers per line and continue on the next line?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sparkey

    How to print 10 prime numbers per line and continue on the next line?

    I have a working code that gives me the prime numbers but I want it to print out either a - if it not a prime and if it is a prime print the prime. I have that working but I can not get it to stop at 10 and then go to the next line. I have tried several diffrent ways of the if (i%10==0) System.out.prin tln(); but to no avail. Please help.
    Here is most the code I have.
    Code:
    	int a=0;// int intializing a=0
    	int b=0;// int intializing b=0
    	
    	public static void main(String... args) 
           {                                   //start main method
    	Scanner input = new Scanner(System.in);//renamed Scanner to input
    	System.out.print("Enter the Starting Number of the Range: ");// print line
    	int a = input.nextInt();// gets starting range from user
    	System.out.print("Enter the Ending Number of the Range  : ");//print line
    	int b = input.nextInt();//gets upper range from user
    		System.out.println(dash);// prints a line of dashes
    		
    	for(int i = a; i <= b; i++) //this is the primer for the prime number formula
             	{
    	boolean isPrime = true;//sets the statement to true
    	if (isPrime)
    	   for(int j = 2; j <= i; j++)//formula to get the prime
                {
    	if(i != j && i % j == 0) //formula checking prime
                isPrime =false; //sets statement to false
                }
    	if (isPrime)
    		   	{
    		
    		System.out.print(i+" ");//if true it prints prime number
    		 System.out.print("-"+" ");//if false it prints a dash
    		 
     		   	}
    	  	}
            
    	 }//end of main method
    }//end of class
    Last edited by Dormilich; Nov 2 '10, 06:27 AM. Reason: please use [CODE] [/CODE] tags when posting code
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Not sure what you mean by
    stop at 10 and then go to the next line
    If you want a line break after so many numbers, then simply keep a tally,
    when tally reaches desired figure, output a newline then reset tally to zero.
    Code:
    tally = 0
    for(............)
    {
        calculations
        tally ++;
        if(tally>10)
        {
           System.out.println();
            tally = 0; 
        }
    }
    But I may have misread the question

    Comment

    • Sparkey
      New Member
      • Nov 2010
      • 1

      #3
      Yes that is what I mean.

      I tried what you had but I have never used the calculations function and I got several syntax errors. I tried to put it after my print line and also I tried to put it after my main method and still got several errors is there a diffrent way using a if statement like if (i=1;i<10;i++) with that type of function.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        I wrote pseudo code not Java.
        This just illustrates a technique you could try.
        calculations is your calculations not a function.
        As a guide my for loop for(........... .) represents your outer loop for(int i = a; i <= b; i++)

        Comment

        Working...