Search code examples
javaeclipseacm-java-libraries

Java using for-loop to produce series of numbers


I am trying to write a collection of for-loops that produce the following series of numbers below. I am trying to accommodate my loops to print each series on the same line, with spaces between each term. I am new to java and got really confused on how exactly I can accomplish it. On the right side are the digits I am increasing the counting by.

 1. 4  5  6  7  8  9  10                (+1)
 2. 6  5  4  3  2  1                    (-1)
 3. 2  4  6  8  10  12  14  16          (+2)  
 4. 19  17  15  13  11  9  7  5         (-2)
 5. 7  15  23  31  39                   (+8)
 6. 2  4  8  16  32  64                 (*2)

Here is the code the way I tried to accomplish it. I got the first row to work but I'm wondering weather there's an easy way I can create the rest of them without re-duplicating the program.

import acm.program.*;

public class ppLoop extends ConsoleProgram {
    public void run()
    {
        {

        for(int row = 1; row < 2; row++)
        {
        print(" " + row + ". ");

        for (int col = 4; col < 11; col++)

            {
            print(row*col + " ");
            } //col values

            println( );
            } //row values

        }

    }
}

I am new to java and right now going over for-loops and trying to accomplish this in for-loop. If someone could help me out, I would really appreciate it. Thank you! Edit: Here is what happens when I increase the number of rows.

enter image description here

Edit: Here is the solution of what I had tried accomplishing. Thanks to everyone who helped me.

import acm.program.*;
public class ppLoop extends ConsoleProgram
{
    public void run()
    {
        {
        for(int i = 1; i < 2; i++) // One iteration of outer loop
             {
               print(i + ". "); // print row number 1

                for (int j = 4; j < 11; j++) // loop for row 1   
                {
                   print(j + " ");
                } 
                println( );
                print((i + 1) + ". ");

                for (int j = 6; j > 0; j--) // loop for row 2
                {
                   print(j + " ");
                }
                println();
                print((i + 2) + ". ");

                for (int j = 2; j < 17; j = j + 2) // loop for row 3
                {
                   print(j + " ");
                }
                 println();
                print((i + 3) + ". ");

                for (int j = 19; j > 4; j = j - 2) // loop for row 4
                {
                  print(j + " ");
                }
                 println();
                print((i + 4) + ". ");

                for (int j = 7; j < 40; j = j + 8) // loop for row 5
                {
                  print(j + " ");
                }
                println();
                print((i + 5) + ". ");

                for (int j = 2; j < 65; j = j * 2) // loop for row 6
                {
                print(j + " ");
                }
                println();
             } 
        } //close outer loop
    } //close public run
} //close console program

Solution

  • You can perform this program with a series of nested loops. I have done the first three rows. I took out your package and used a main method. Also, your indentation was very confusing. Since your increment changes each line, I don't know of a way to make it any shorter than this using for loops.

    public class ppLoop{
       public static void main(String[] args)
       {
          {
    
             for(int i = 1; i < 2; i++) // One iteration of outer loop
             {
                System.out.print(i + ". "); // print row number
    
                // you can use the same variable for each inner loop    
                for (int j = 4; j < 11; j++) // loop for row 1   
                {
                   System.out.print(j + " ");
                } 
    
                System.out.println( );
                System.out.print((i + 1) + ". ");
                for (int j = 6; j > 0; j--) // loop for row 2
                {
                   System.out.print(j + " ");
                }
    
                System.out.println();
                System.out.print((i + 2) + ". ");
                for (int j = 2; j < 17; j = j + 2) // loop for row 3
                {
                   System.out.print(j + " ");
                }
             } 
          }
       }
    }