Colour Theme Font Size Options
 
 
 
Exercise 3

Nested Counting Loops Exercise

This is a loop nesting exercise. Use two loops, including at least one for loop, to write a C++ program that prints out an upside down triangle of stars to the screen.

Write a program from scratch

  • Write a C++ program named ex3_stars.cpp in the Visual Studio project or create it in your IDE.
  • The user will enter the number of stars in the first line, then nested loops will print the triangle of stars.
    For example:
      
    How many stars on the first line? 5 ***** **** *** ** *
    or:
      
    How many stars on the first line? 2 ** *

Tips:

  • try starting with a counting for loop that counts down from the number the user entered to 1 like this:
  •     
    How many stars on the first line? 5 5 4 3 2 1
  • then add an inner loop that can print that many stars on one line.
  • each loop will have its own counting variable - make sure it has a unique name

When you are done:

  • Take screenshots demonstrating two different sizes of star triangle.
  • For this exercise, you will have: ex3_stars.cpp.
  • If you have completed Exercises 1,2 and 3, congratulations! Option 1 is complete! Please upload .cpp files and screenshots to URCourses.