C Program to Print a 180° Rotated Pyramid

C Program to Print a 180° Rotated Pyramid

A school pattern exercise asks for right-aligned star triangle. Describe what one row contains before putting that row inside the outer loop.

A right-aligned triangle decreases its leading spaces while increasing its stars.

Before this exercise, review loop counters and stopping conditions. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why right-aligned star triangle uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Print exactly two rows and predict both the characters and the leading spaces.

Right-Aligned Star Triangle: follow the data

  1. Given data

    A school pattern exercise asks for right-aligned star triangle. Describe what one row contains before putting that row inside the outer loop.

  2. Rule

    A right-aligned triangle decreases its leading spaces while increasing its stars.

  3. Observed result

    * ** ***

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

For three rows the leading spaces are two, one and zero; the stars are one, two and three.

The outer loop chooses a row; an inner loop chooses positions within that row. A newline belongs after the entire row, rather than after every printed character. Spaces are output data too, so count them explicitly.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int rows = 3;
  for (int row = 0; row < rows; row++) {
    for (int s = 0; s < rows - row - 1; s++) putchar(' ');
    for (int col = 0; col <= row; col++) putchar('*');
    putchar('\n');
  }
  return 0;
}

Expected output

  *
 **
***
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int rows = 3;
  for (int row = 0; row < rows; row++) {
    for (int s = 0; s < rows - row - 1; s++) putchar(' ');
    for (int col = 0; col <= row; col++) putchar('*');
    putchar('\n');
  }
  return 0;
}

The sample requests 3 rows, with indexes zero through 2.

For three rows the leading spaces are two, one and zero; the stars are one, two and three.

Follow the worked calculation

  1. Start with the sample

    The sample requests 3 rows, with indexes zero through 2.

  2. Apply the rule

    For three rows the leading spaces are two, one and zero; the stars are one, two and three.

  3. Check the result

    * ** ***

    *
     **
    ***
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int rows = 3;
  for (int row = 0; row < rows; row++) {
    for (int s = 0; s < rows - row - 1; s++) putchar(' ');
    for (int col = 0; col <= row; col++) putchar('*');
    putchar('\n');
  }
  return 0;
}

Print exactly two rows and predict both the characters and the leading spaces.

Worked answer

The two-row answer reuses the row rule with a smaller requested count. Read the monospaced output: a leading space changes alignment even though it does not look like a printed symbol.

C17 · complete program

#include <stdio.h>
int main(void) {
  int rows = 2;
  for (int row = 0; row < rows; row++) {
    for (int s = 0; s < rows - row - 1; s++) putchar(' ');
    for (int col = 0; col <= row; col++) putchar('*');
    putchar('\n');
  }
  return 0;
}

Expected output

 *
**
Run in C compiler →

Recognize the important boundaries

Restrict row counts to a practical positive limit before calculating widths or generating large output.

The old 1800-rotation URL is preserved. For its right-aligned triangle lesson, the title names the actual output: a true 180-degree rotation would need its own explicit row-and-column transformation.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Where should the row’s newline be printed?
Choose one answer. You can try again.

Answer and explanation

After the inner loops finish the row.

Correct. The value or separator must belong to the correct level of repetition.

Use the idea independently

A right-aligned triangle decreases its leading spaces while increasing its stars.

Continue with nested loop control.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Print Inverted Pyramid →