How to Display a Right Triangle of Numbers in C Programming

Written by luis olortegui
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Display a Right Triangle of Numbers in C Programming
Displaying a right triangle of numbers is a basic programming assignment. (Ablestock.com/AbleStock.com/Getty Images)

Unlike most software applications, programming languages can help you unlock the full power of your computer by allowing you to execute custom-made tasks. Displaying a right triangle of numbers is a perfect example for beginners, as it uses many key concepts such as loops, constants and variables, conditions, etc. The right triangle of numbers program also allows for user-specific modifications, so you can change your code to add or remove features.

Skill level:
Easy

Other People Are Reading

Things you need

  • C compiler (any version; recommended: Visual C++ express)

Show MoreHide

Instructions

  1. 1

    Visualise your desired output. The display for a right triangle of numbers looks like this:

    1

    1 2

    1 2 3

    1 2 3 4 [and so on]

  2. 2

    Identify your user input (if any). In this case, the user chooses the number of lines of the triangle, so we need to request the number of lines from the user.

  3. 3

    Check if the number of lines is appropriate for the output. A minimum and maximum number of lines are required; otherwise, errors might occur. For example, the user input could be -1, but we can't draw a triangle with -1 lines. In this example, we will set 2 as minimum number of lines and 20 as maximum number of lines.

  4. 4

    Look for repetitions and design loops based on them. In this project there are two loops: one that draws all the lines from line 1 to the last line, and a second that draws the elements on each line and resets each time a new line begins to be drawn. This element loop is nested inside the line loop.

  5. 5

    Put all your algorithm steps together. It looks like this:

    1. Ask for user input

    2. Check for boundaries (minimum and maximum)

    3. Draw lines loop

    4. Draw elements on each line loop (nested inside draw lines loop)

    5. End program

  1. 1

    Set up your main program. Since we are asking the user for input, use the iostream library.

    The first lines of code are:

    include <iostream>

    using namespace std;

    int main(){

  2. 2

    Declare variables and constants. We need one variable and two constants: one for the user input (called "lines") and one constant for the minimum bound (min_lines) and maximum bound (max_lines). All of these should be integers. The code:

    int lines;

    const int min_lines=2;

    const int max_lines=20;

  3. 3

    Ask your user for the number of lines. Store the user input in the lines variable:

    cout << "Enter the number of lines: ";

    cin >> lines;

    cout << endl << endl;

  4. 4

    Apply the check to verify that the user input is within bounds. If it is outside bounds, correct it by setting it to the closest bound value. The code is as follows:

    if ( lines < min_lines )

    { lines=min_lines;

         cout &lt;&lt; "Too few lines. Changed to " &lt;&lt; min_lines ,&lt;&lt;" lines" &lt;&lt; endl &lt;&lt; endl; }
    

    else if ( lines > max_lines)

    { lines=max_lines;

         cout &lt;&lt; "Too many lines. Changed to " &lt;&lt; max_lines ,&lt;&lt;" lines" &lt;&lt; endl &lt;&lt; endl; }
    
  5. 5

    Start the loop that draws the lines. Since we know the number of lines won't change after user input, we can use a FOR loop. Leave the loop open to nest the loop that draws each number on the line. Here is the code:

    for ( int count1=1; count 1 == lines; count 1++ )

    {

  6. 6

    Insert our second nested loop to draw the elements on each line. Type the code:

                    for ( count 2 = 0; count 2 == count 1 ; count 2++ )
    
                          { cout &lt;&lt; (count2+1) &lt;&lt; " "; }
    
  7. 7

    Close our line loop. Every time we finish displaying a line, order the computer to send our cursor to the next line. Also, add a space after our triangle has been fully displayed. This is the code:

    cout << endl;

    }

    cout << endl;

  8. 8

    Finish our program by returning a value to our main function.

    return(0);

    }

  9. 9

    Type the complete code, and your program is ready.

    Here is the complete code:

    include <iostream>

    using namespace std;

    int main(){

    int lines;

    const int min_lines=2;

    const int max_lines=20;

    cout << "Enter the number of lines: ";

    cin >> lines;

    cout << endl << endl;

    if ( lines < min_lines )

    { lines=min_lines;

               cout &lt;&lt; "Too few lines. Changed to " &lt;&lt; min_lines ,&lt;&lt;" lines" &lt;&lt; endl &lt;&lt; endl; }
    

    else if ( lines > max_lines)

    { lines=max_lines;

               cout &lt;&lt; "Too many lines. Changed to " &lt;&lt; max_lines ,&lt;&lt;" lines" &lt;&lt; endl &lt;&lt; endl; }
    

    for ( int count1=1; count 1 == lines; count 1++ )

    {

               for ( count 2 = 0; count 2 == count 1 ; count 2++ )
    
                          { cout &lt;&lt; (count2+1) &lt;&lt; " "; }
    

    cout << endl;

    }

    cout << endl;

    return(0);

    }

Tips and warnings

  • Check carefully for typing mistakes. Even something really small (like replacing a ";" with a ",") will cause a big error.
  • Use an editor (such as Visual C++ -; see Resources) to help you with the typing and make the coding process easier.
  • Feel free to modify some of the parameters from the code in order to customise your triangle a little more. Changing min_lines and max_lines will increase and decrease the size of your triangle.
  • It is best practice to always write an algorithm before we write any code (even for small/simple projects).

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.