Thursday, October 23, 2014



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


Chapter 2 Exercise 7

Write nested for loops to produce the following output:

      5
   5 5
5 5 5
   5 5
      5

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

package ch2_ex7;
public class Ch2_Ex7 
{
    public static void main(String[] args) 
    {
        grup1();
        grup2();
        grup3();
        grup2();
        grup1();
    }

public static void grup1(){
        for (int line = 1; line <=1; line = line+1)
        {
            for (int space = 1;  space<=4; space = space+1)
            {
                System.out.print(" ");
            }
            for (int five = 1; five <=1; five = five+1)
            {
                System.out.print("5");
            }
            System.out.println("");
        }
    } 

public static void grup2()
{
      for (int line = 1; line<=1; line = line+1)
        {
            for (int space = 1;  space<=2; space = space+1)
            {
                System.out.print(" ");
            }  
            for (int five = 1; five <=1; five = five+1)
            {
                System.out.print("5");
            }
            for (int space = 1; space <=1; space = space+1)
            {
                System.out.print(" ");
            }
            for (int five = 1; five <=1; five = five+1)
            {
                System.out.print("5");
            }
            System.out.println("");
         
        }
}

public static void grup3()
{
    for (int line = 1; line <= 1; line=line+1)
    {
        for (int five = 1; five <=1; five = five+1)
            {
                System.out.print("5");
            }
        for (int space = 1;  space<=1; space = space+1)
            {
                System.out.print(" ");
            }  
            for (int five = 1; five <=1; five = five+1)
            {
                System.out.print("5");
            }
            for (int space = 1; space <=1; space = space+1)
            {
                System.out.print(" ");
            }
            for (int five = 1; five <=1; five = five+1)
            {
                System.out.print("5");
            }
        System.out.println("");
    }
}

}

0 comments:

Post a Comment