6- Nesting Answers

1) Print out the following shapes: \/ \/\/ \/\/\/ \/\/\/\/ \/\/\/\/\/

for (int i = 1; i<6; i++)
{
    for(int j = 0; j<i; j++)
    {
    System.out.print("\\/");
    }
    System.out.print(" ");
}

2) Print out the following 54321,4321,321,21,1

for (int i = 5; i>0; i--)
{
    for(int j =i; j>0; j--)
    {
    System.out.print(j);
    }
    System.out.print(" ");
}

3) Print out the following shapes */ */ */ */ *****/

for (int i = 1; i<6; i++)
{
    System.out.print("\\");
    for(int j = 0; j<i; j++)
    {
    System.out.print("*");
    }
    System.out.println("/");
}

4) Print out a 10 x 10 table square

for (int i = 1; i<11; i++)
{
    for(int j = 1; j<11; j++)
    {
    System.out.print("\t"+ i*j + "\t |");
    }
    System.out.println();
}

5) Print out the following shapes \/ \// \\/// \\//// \\\/////

String seed = "";
for (int i = 1; i<6; i++)
{
    seed = "\\" + seed + "/";
    System.out.print(seed + " ");
}

6) Print out an 8 x 8 chessboard. Use * for black and – for white

boolean isBlack = true;
for (int i = 1; i<9; i++)
{
    for(int j = 1; j<9; j++)
    {
    System.out.print(isBlack ? "*" : "-");
    isBlack = !isBlack;
    }
    isBlack=!isBlack;
    System.out.println();
}

7) Print out the following shapes:

*
**
**
***
***
***
****
****
****
****
*****
*****
*****
*****
*****
String stars = "";
for (int i = 1; i<6; i++)
{
    stars = stars + "*";
    for(int j = 0; j<i; j++)
    {
    System.out.println(stars);
    }
    System.out.println();
}

Leave a Comment