OK here is my code to print all prime numbers less than 100 and also print the first 36 numbers in the fibonacci series. I want to change it so that the fibonacci prints 9 numbers per line and the prime numbers print in 5 per line. Im new to learning loops and nested loops and cant seem to figure this out. thanks, lee

``````//course: CSC 190;
//project:lab9;
//date:3/25/201;
//purpose:Print all prime numbers less than 100 and print first 36 fibonacci numbers
//author:

class main
{

void fib()
{
int n0 = 1, n1 = 1, n2; // Initialize variables
System.out.print(n0 + " " + n1 + " "); //Print first and second terms of the series
for (int y = 0; y < 34; y++)
{ // Loop for the next 34 terms
n2 = n1 + n0; // Next term is sum of previous two
System.out.print(n2 + " "); // Print it out
n0 = n1; // First previous becomes 2nd previous
n1 = n2; // And current number becomes previous
}
System.out.println(); // Terminate the line
}

void prime()
{
int p;
for(int i=2;i<100;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
System.out.print(i+" ");
}
}

}

class hw4
{
public static void main(String [] args)
{
main M = new main();

M.fib();
System.out.println();
M.prime();

}

}
``````

## All 6 Replies

Have a counter in each of those methods. When you print something increase it by one. When it reaches 5 or 9, call the `System.out.println();` and set it to 0.

``````int count = 0;
for (loop) {
System.out.print("something ");
count++;
if (count==5) {
count=0;
System.out.println();
}
}``````

This is where the % function is handy. If you want to print out n number per line...within the loop use System.out.print and add an if statement

``if ( loopcontrolvariable % n == 0 ) System.out.println("");``

in one case your loopcontrolvariable is y in the other loop it is j.

alright thanks guys i got it to work using the first method, now i have one more question. how can i right align my print out of the prime numbers? like inserting a blank space before any single digit number so it will be all right aligned.

``````class main
{

void fib()
{
int n0 = 1, n1 = 1, n2, counter; // Initialize variables
counter =2;
System.out.print(n0 + " " + n1 + " "); //Print first and second terms of the series
for (int i = 0; i < 34; i++)
{ // Loop for the next 34 terms
n2 = n1 + n0; // Next term is sum of previous two
System.out.print(n2 + " "); // Print it out
n0 = n1; // First previous becomes 2nd previous
n1 = n2; // And current number becomes previous
counter+=1;
if(counter % 9 == 0)System.out.println();

}
System.out.println();

}

void prime()
{
int p, counter;
counter = 0;
for(int i=2;i<100;i++)
{

p=0;
for(int j=2;j<i;j++)
{

if(i%j==0)
p=1;
}
if(p==0)
{
counter+=1;
System.out.print(i+" ");
if(counter % 5 == 0)System.out.println();
}

}
}
}
class hw4
{
public static void main(String [] args)
{
main M = new main();

M.fib();

M.prime();

}
}``````

You can do the same thing you did from your first few printouts... but you do it before the end of the loop so every time it calls for a loop it will add a space before it prints another

System.out.print(" ");// this will give you pace

a program that prints the numbers 1 to 100 on the screen in rows of 10
numbers per line.

Hello yogita.
Welcome to DaniWeb, but please read the DaniWeb member rules before posting
http://www.daniweb.com/community/rules

In particular:
Do not hijack old forum threads by posting a new question as a reply to an old one
and
Do provide evidence of having done some work yourself if posting questions from school or work assignments

Be a part of the DaniWeb community

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