468,773 Members | 2,517 Online

How to get 10 values per line in output?

I have been stuck on an assignment; hopefully you can help me out.
I have posted the code below; basically I need to output n into rows of 10 answers per line.
I cannot use arrays! I am able to output the answers into a continuous row, but cannot seem to break it into the requirement above.
Expand|Select|Wrap|Line Numbers
1. class PrimeFinder {
2.     public static void main(String[] args) {
3.         Boolean prime;
4.         for (int n = 2; n < 100; n++) {
5.             prime = true;
6.             /** screening out even numbers, since it's multiple of 2 */
7.             if ((n % 2) == 0)
8.                 prime = false;
9.             /** screening for odd numbers to find Prime */
10.             else {
11.                 for (int x = 3; x <= (n / 2); x+=2) {
12.                     if ((n % x) == 0) {
13.                         prime = false;
14.                         break;
15.                     }
16.                 }
17.             }
18.             if (prime)
19.                 System.out.print(n + " ");
20.             else
21.                 continue;
22.         }
23.     }
24. }
~newgal
Jan 21 '08 #1
9 12829
1,216 Expert 1GB

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Jan 21 '08 #2
1,216 Expert 1GB
Introduce an int variable whose value is the number of primes printed in the current row. When it reaches 10, reset it to 0 and:
Expand|Select|Wrap|Line Numbers
1. System.out.println();
Jan 21 '08 #3
Introduce an int variable whose value is the number of primes printed in the current row. When it reaches 10, reset it to 0 and:
Expand|Select|Wrap|Line Numbers
1. System.out.println();
How do I do that? Where should I insert the int variable?
Sorry I am really lost on this one.
Jan 21 '08 #4
1,216 Expert 1GB
How do I do that? Where should I insert the int variable?
Sorry I am really lost on this one.
You have only written on method (main), so there doesn't seem to be many places to define the variable. As an exercise, can you write a little program that produces this output using a single loop and using my suggestion?

[HTML]
.
0 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 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99
[/HTML]

If you can do that, you'll understand how to add this feature to your prime program.
Jan 21 '08 #5
Laharl
849 Expert 512MB
Create the int value outside the function (set it to 0), and every time you print (or locate, up to you) a prime, increment it. Once you hit 10, reset the int to 0 and use System.out.println(); to print a newline character.
Jan 21 '08 #6
If you can do that, you'll understand how to add this feature to your prime program.
OK, I've been thinking about this program and I can write it out and it output, but again, I can only output it continuously.
I understand the concept that I need to count the output of prime to 10, then reset, but I am not sure how to do it. (Sorry this is my first programming class and I'm struggling :(
Do I use for loops to do it? I tried that route, didn't work!

~newgal
Jan 22 '08 #7
1,216 Expert 1GB
OK, I've been thinking about this program and I can write it out and it output, but again, I can only output it continuously.
I understand the concept that I need to count the output of prime to 10, then reset, but I am not sure how to do it. (Sorry this is my first programming class and I'm struggling :(
Do I use for loops to do it? I tried that route, didn't work!

~newgal
Here's a skeleton solution. Use count to decide when to start a new line.
Expand|Select|Wrap|Line Numbers
1. public class Example {
2.     public static void main(String[] args) {
3.         int count = 0;
4.         for(int i=0; i<100; ++i) {
5.             System.out.print(i + " ");
6.
7.             if (*expression*) {
8.                 System.out.println();
9.
10.             }
11.         }
12.     }
13. }
Jan 22 '08 #8
Oh! I get it now....thanks so much. Now both my program works and so did the little one.
Jan 22 '08 #9