CS110 Lab 11

Nested for loop

  1. Write a for loop that will display the letter 'X'   n number of times on a single line, where   n is an integer between 10 and 20.
  2. Revise this code to repeat the output produced by #1   y times where   y is an integer between 4 and 10.

    For example, given   n = 15 and   y = 6, the following output will appear:

        XXXXXXXXXXXXXXX
        XXXXXXXXXXXXXXX
        XXXXXXXXXXXXXXX
        XXXXXXXXXXXXXXX
        XXXXXXXXXXXXXXX
        XXXXXXXXXXXXXXX


  3. Suppose that you have two nested loops. The inner loop executes 5 times and the outer loop executes 10 times. How many total iterations are there within the nested loop structure?


  4. Write a method left_align() that receives a positive integer n and produces the following output.
        n: 5
        X
        XX
        XXX
        XXXX
        XXXXX


  5. Write a method right_align() that receives a positive integer n and produces the following output.
        n: 5
        OOOOX
        OOOXX
        OOXXX
        OXXXX
        XXXXX




  6. Write a method tree() that receives a positive integer n and produces the following output.
        n: 4
        OOOXOOO
        OOXXXOO
        OXXXXXO
        XXXXXXX





Writing a Program

Write a program that generates a bull'seye using the letter 'X'. You will need a nested for loop for this.