1

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
Dmitriy
  • 97
  • 3
  • 15
  • do you know the starting number of each row? because then its easy but other than that i have no clue whats going on to the different rows – 3kings Oct 28 '15 at 02:57
  • Your saying you want to write 6 different for-loops that printout different increments of numbers? – coderrick Oct 28 '15 at 02:57
  • @coderrick I don't know if there's a way doing so within one loop. – Dmitriy Oct 28 '15 at 03:02
  • @3kings In the assignment, different rows have different amount of numbers. I'm just a bit lost here how to accomplish what I am looking for. – Dmitriy Oct 28 '15 at 03:08
  • @trooper this is an example loop of what I am trying to accomplish. – Dmitriy Oct 28 '15 at 03:12
  • I think you need a function with following input parameters: number of elements in series and pattern – Atul Kumbhar Oct 28 '15 at 03:26

3 Answers3

5

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 + " ");
            }
         } 
      }
   }
}
  • Thank you that looks exactly what I have tried to accomplish but in a console program. I'll try to modify and make it work. Thanks again! – Dmitriy Oct 28 '15 at 03:44
  • could you please tell me where does the number of line changes, that's where I'm a bit lost. Thanks! – Dmitriy Oct 28 '15 at 04:05
  • I made it work with works but the line counting number stays at 3 for line 4-6. Here's the output I am getting. 1. 4 5 6 7 8 9 10 2. 6 5 4 3 2 1 3. 2 4 6 8 10 12 14 16 3. 19 17 15 13 11 9 7 3. 7 15 23 31 39 3. 2 4 8 16 32 64 Thanks! – Dmitriy Oct 28 '15 at 04:31
  • I changed the line number by printing i incremented by whatever number was necessary before each for loop. If it says three every time, that is because each print statement after the third all say i + 2.. They need to say i + 3, i + 4, i + 5, etc.... – nathankurzz Oct 28 '15 at 21:17
0

You could create a method that takes:
1. A start number
2. What math operation to perform (add, subtract, or multiply)
3. What number to increment/decrement or multiply by
4. An end number

It would look similar to this:

public void formattedFor(int startNum, String operation, int num, int endNum) {
    if (operation.equals("add")) {
        for (int i = startNum; i < endNum; i += num) {
            System.out.print(i + " ");
        }
    }
    if (operation.equals("sub")) {
        for (int i = startNum; i > endNum; i -= num) {
            System.out.print(i + " ");
        }
    }
    else if (operation.equals("mult")) {
        for (int i = startNum; i < endNum; i *= num) {
            System.out.print(i + " ");
        }  
    }
    System.out.println( );   
}
  • @Thanks! We haven't gone over methods yet. The library I'm most familiar with is cam library. I'll try it out. – Dmitriy Oct 28 '15 at 03:27
0

If I'm understanding the problem, you want to print six series that each start with a different number and increment/decrement that number by some value. Since I see no relationship between the initial value and the increment/decrement, you're going to have to write six separate for loops.

If you're absolutely averse to this, you can store your initial values, your increments/decrements, and your final values in an array and iterate through them using a for loop, an if statement (to deal with the multiplication) and a while loop. The array would look like this:

int[][] values = new int[][] {
        {4, 6, 2, 19, 7, 2},
        {1, -1, 2, -2, 8, 2},
        {10, 1, 16, 5, 39, 64}
    };

I could write up the source based on this, but it's not what you asked for.

I strongly suspect that, if this is a homework assignment and you've modified the problem, there's something you've failed to understand about the problem itself. If this is meant to have an simple solution that uses for loops, there should probably be some logic that binds the rows together, unless you're allowed to use arrays/while loops/for loops/objects and methods.

On another note, you should format your code differently. It's somewhat difficult to read right now. In general, indent things that happen inside loops, classes, or functions. For example:

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
    }
}
the spectre
  • 350
  • 4
  • 11
  • The first answer shows what I am trying to accomplish but in console program. I'm sorry that it turns out to be a bit confusing. I'll try to edit to show it. – Dmitriy Oct 28 '15 at 04:14
  • If the first answer is what you're trying to do, then my array-based solution would also work, if you wanted to do it. – the spectre Oct 28 '15 at 04:24
  • spectra I'll try it out and see what I can get thanks! I'm open to the options and trying to understand the logic behind this – Dmitriy Oct 28 '15 at 04:27