C++ Number Triangle Pattern (Odd/Even Rows)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An alternating odd/even number triangle grows one number per row. Odd rows print odds starting at 1; even rows print evens starting at 2 — each step advances with k += 2.

Remember
Rule: for i from 1 to rows
        k = 1 if i odd, else k = 2
        print i numbers: k, then k += 2

1
2 4
1 3 5
2 4 6 8
1 3 5 7 9     ← rows = 5

Follows the left-shifted odd triangle in Program 17; next is the fill-with-5 triangle in Program 19.

How to Solve It

Outer loop grows width i. Set start k from i % 2, then print i values with k += 2 after each print.

MethodIdeaBest for
Parity + k += 2Odd row → k = 1; even row → k = 2; then step by 2Learning, interviews, exams
Ternary startk = (i % 2 == 0) ? 2 : 1;Same logic in one line

Pseudocode

Pseudocode
for i from 1 to rows:
    if i is even:
        k = 2
    else:
        k = 1
    for j from 1 to i:
        print k and a space
        k += 2
    print newline

Cheat sheet

GoalPattern
Grow each rowfor (i = 1; i <= rows; i++)
Pick startk = (i % 2 == 0) ? 2 : 1;
Print sequencecout << k << " "; k += 2;
End of rowcout << "\n";
Flip parityk = (i % 2 == 0) ? 1 : 2;

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << k << " "Stays on the same lineEach number on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

Print numbers without a newline, then end the row once.

Live Preview

Change the row count and the alternating odd/even triangle updates instantly — capped at 9 for readable demos.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result rows = 5 · 15 numbers
1
2 4
1 3 5
2 4 6 8
1 3 5 7 9

Worked Walkthrough — rows = 5

Trace how parity sets the start and k += 2 keeps each row odd-only or even-only.

iStart kSequencePrints
1111
222, 42 4
311, 3, 51 3 5
422, 4, 6, 82 4 6 8
511, 3, 5, 7, 91 3 5 7 9

Total numbers = n(n+1)/2 → O(n²). Reset k at the start of every row.

C++ Programs

Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — set k from parity, then print i values with k += 2.

C++
#include <iostream>
using namespace std;

int main()
{
    int rows = 5;
    int i, j, k;

    for (i = 1; i <= rows; i++)
    {
        if (i % 2 == 0)
            k = 2;
        else
            k = 1;

        for (j = 1; j <= i; j++)
        {
            cout << k << " ";
            k += 2;
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer grows the row. i is both the row index and how many numbers to print.

2. Parity sets the start. Odd i → k = 1; even i → k = 2.

3. Step and newline. Print k, then k += 2; call cout << "\n" after the inner loop.

Example 2 — User Input Rows

Read rows with cin, validate, then use a ternary for the start value.

C++
#include <iostream>
using namespace std;

int main()
{
    int rows;
    int i, j, k;

    cout << "Enter the number of rows: ";
    if (!(cin >> rows) || rows <= 0)
    {
        cout << "Please enter a positive integer.\n";
        return 1;
    }

    for (i = 1; i <= rows; i++)
    {
        k = (i % 2 == 0) ? 2 : 1;

        for (j = 1; j <= i; j++)
        {
            cout << k << " ";
            k += 2;
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Same core. Ternary start + k += 2 matches Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable output:

Safer input
if (!(cin >> rows) || rows < 1 || rows > 9)
{
    cout << "Enter a whole number from 1 to 9.\n";
    return 1;
}

Example 3 — Compact rows = 3

Same structure with only three rows — easy to confirm odd vs even starts.

C++
#include <iostream>
using namespace std;

int main()
{
    int rows = 3;
    int i, j, k;

    for (i = 1; i <= rows; i++)
    {
        k = (i % 2 == 0) ? 2 : 1;

        for (j = 1; j <= i; j++)
        {
            cout << k << " ";
            k += 2;
        }
        cout << "\n";
    }

    return 0;
}

How It Works

1. Three rows. i = 1 → 1; i = 2 → 2 4; i = 3 → 1 3 5.

2. Trace on paper. If you use k++ instead of k += 2, odds and evens mix on the same row.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for five rows or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

k++

Mixed parity on a row

Using k++ instead of k += 2 mixes odd and even numbers. Keep the step of 2.

carry k

Forgot to reset k

Set k fresh from i % 2 at the start of every outer iteration — do not carry it from the previous row.

\n inside

Broken rows

If cout << "\n" sits inside the inner loop, each number lands on its own line. Call it only after the row finishes.

rows = 1

Single number

Output is just 1 — a good sanity check for input validation.

no space

Digits glued together

Use cout << k << " " so multi-digit values stay readable as the rows grow.

cin

Check the stream

Validate cin >> rows before looping — a failed read leaves rows unset or stale.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Total numbers printed = 1 + 2 + … + n = n(n+1)/2 → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Parity picks start: odd row → k = 1; even row → k = 2.
  • Step by 2: k += 2 keeps each row odd-only or even-only.
  • Break the row: cout << k << " " in the inner loop; cout << "\n" after it.
  • Complexity: O(n²) time from n(n+1)/2 numbers; O(1) extra space.

One line: for each row i, set k from parity, print i values stepping by 2, then newline.

Frequently Asked Questions

An alternating odd/even triangle: for rows=5 you get 1 / 2 4 / 1 3 5 / 2 4 6 8 / 1 3 5 7 9 — odd rows print odds, even rows print evens.
We check i % 2. If i is odd, set k = 1; if i is even, set k = 2.
Row 2 is even, so k starts at 2. The inner loop prints k then adds 2 twice: 2, then 4.
After printing k, update k += 2 so the sequence stays odd or even while increasing.
Yes. Swap the assignment so even rows start at 1 and odd rows start at 2.
cout << k << " " stays on the same line with a space. cout << "\n" ends the current line. Numbers use cout; the row break uses cout << "\n" after the inner loop.
After prompting, use if (!(cin >> rows) || rows <= 0) to reject bad input before printing.
O(n²) for n rows because total numbers printed are 1+2+…+n = n(n+1)/2.

Did you know?

Row parity picks the start value: odd rows begin at 1, even rows at 2. Then k += 2 keeps each row odd-only or even-only — still O(n²) total prints for n rows.

Next: Fill with 5 Triangle

Continue with ascending prefixes padded by 5 to fixed width.

Program 19 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful