473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.println(); but to no avail. Please help.
Here is most the code I have.
Expand|Select|Wrap|Line Numbers
  1.     int a=0;// int intializing a=0
  2.     int b=0;// int intializing b=0
  3.  
  4.     public static void main(String... args) 
  5.        {                                   //start main method
  6.     Scanner input = new Scanner(System.in);//renamed Scanner to input
  7.     System.out.print("Enter the Starting Number of the Range: ");// print line
  8.     int a = input.nextInt();// gets starting range from user
  9.     System.out.print("Enter the Ending Number of the Range  : ");//print line
  10.     int b = input.nextInt();//gets upper range from user
  11.         System.out.println(dash);// prints a line of dashes
  12.  
  13.     for(int i = a; i <= b; i++) //this is the primer for the prime number formula
  14.              {
  15.     boolean isPrime = true;//sets the statement to true
  16.     if (isPrime)
  17.        for(int j = 2; j <= i; j++)//formula to get the prime
  18.             {
  19.     if(i != j && i % j == 0) //formula checking prime
  20.             isPrime =false; //sets statement to false
  21.             }
  22.     if (isPrime)
  23.                {
  24.  
  25.         System.out.print(i+" ");//if true it prints prime number
  26.          System.out.print("-"+" ");//if false it prints a dash
  27.  
  28.                 }
  29.           }
  30.  
  31.      }//end of main method
  32. }//end of class
Nov 2 '10 #1
3 7542
code green
1,726 Expert 1GB
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.
Expand|Select|Wrap|Line Numbers
  1. tally = 0
  2. for(............)
  3. {
  4.     calculations
  5.     tally ++;
  6.     if(tally>10)
  7.     {
  8.        System.out.println();
  9.         tally = 0; 
  10.     }
  11. }
But I may have misread the question
Nov 2 '10 #2
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.
Nov 2 '10 #3
code green
1,726 Expert 1GB
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++)
Nov 3 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Don | last post by:
> How do you write a program to print the prime numbers > up to 1 million? #include <stdio.h> /* This program implements a blindingly fast O(n^n) algorithm to find prime numbers, using an...
3
by: triplejump24 | last post by:
Hey. Im trying to make program that basically displays all the prime numbers. I need to use bool and for but im not quite sure if i have this right. So far i have this bunch of a mess but can...
25
by: johnmsimon | last post by:
i need to develop a code that finds a prime right number between 2 and 100000. and print one line of text that indicates if the int. is right prime. i am in beginning programing so complex is...
7
by: newstips6706 | last post by:
1, 2, 3, 5, 7... PRIME Numbers ________________________________ Definitions What is a PRIME Number ?
11
by: flyaway888 | last post by:
Hey All, I need a program to input a number, test if it is a prime number or not and then output it if it is prime or output the next prime number if it is not. So far I have to input section, the...
5
by: trillianx | last post by:
I know there has been lot of discussion on Prime numbers program but I have very specific question. Here is my program: # Find the prime numbers # This is a user input program that lets you...
11
by: aidasa2001 | last post by:
I need a program which can say the number (input)is prime and then write other prime numbers with that digits. eg.for 13 print the number is prime and print 113( another prime num with that digits)...
4
by: Villanmac | last post by:
Hi i have made the following program to make a fucntion that determines if a number is prime. Than i am supposed to use this function to print all prime numbers between 1 and 10000. What am I...
8
by: cokofreedom | last post by:
I was reading up on this site http://www.noulakaz.net/weblog/ 2007/03/18/a-regular-expression-to-check-for-prime-numbers/] of an interesting way to work out prime numbers using Regular Expression....
12
by: electric916 | last post by:
I have a homework assignment i Am totally confused on. I started with a basic code to determine if a number is prime or not, but need guidance from here. I will post assignment details then what I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.