Skip to main content

Pattern: shrinking "Programming"

Short answer

Loop i=0 to 5; inner loop prints characters from index i to 10-i of the string "Programming" using putchar. Each row drops one character from both ends, yielding the shrinking pattern from "Programming" down to "a".

Write a C program to display following pattern without using formatted input/output statements.

Programming
rogrammin
ogrammi
gramm
ram
a

Each new row drops one character from the beginning and one from the end of "Programming". Row i (starting from 0) prints the characters from index i to 10 - i. The output is done with putchar() only.

#include <stdio.h>
int main(void)
{
    char s[] = "Programming";
    int i, j;
    for (i = 0; i < 6; i++)
    {
        for (j = i; j < 11 - i; j++)
            putchar(s[j]);
        putchar('\n');
    }
    return 0;
}

Output:

Programming
rogrammin
ogrammi
gramm
ram
a

The outer loop runs 6 times (11 characters become 9, 7, 5, 3 and 1); the inner loop prints characters from s[i] to s[10-i].

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Programming chapter 4 questions →