Thursday, October 23, 2014



Building Java Programs A Back To Basics Approach - 3rd Edition
by Stuart Reges and Marty Stepp


Chapter 2 Exercise 8

Write nested for loops to produce the following output:

55555
  4444
    333
      22
        1

/* Copyright Mihai George Forlafu @ 2014
 * Copenhagen School of Design and Technology
 * Building Java Programs A Back To Basics Approach 5th edition
 * Chapter 2 Exercise 8, page 157
 */

package ch2_ex8;
public class Ch2_Ex8 
{
    public static void main(String[] args) 
    {
       for (int line = 5; line>=1; line--)
        {
            for (int space = 0; space<=4-line; space++)
            {
                System.out.print(" ");
            }
            for (int num = 1; num<=line; num++)
            {
                System.out.print(line);
            }
            System.out.println("");
        } 
    }
    
}

1 comment:

  1. How would you program this with a double-nested for-loop?

    ReplyDelete