Dmitriy
Dmitriy

Reputation: 97

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

Upvotes: 1

Views: 4221

Answers (3)

the spectre
the spectre

Reputation: 350

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
    }
}

Upvotes: 0

nathankurzz
nathankurzz

Reputation: 66

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 + " ");
            }
         } 
      }
   }
}

Upvotes: 5

Ashley Dumaine
Ashley Dumaine

Reputation: 1

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( );   
}

Upvotes: 0

Related Questions