Hi all.
I am new in C language. I am learning to program by using C for my first step. Now I am reading a chapter about looping (while, for, do.. while, nested loop).

In my exercise book, the question is about to:
Using nested loop to print out this one,

678cd8de74d71462869e5d8593263743

But, I managed to print like this one,

d4a699824481fc0c48c1c5809093577a

by using this code,

#include <stdio.h>
int main(void)
{
    int x;
    int y;
    for(x=1; x<=5; ++x)
    {
        for(y=x; y<=5; ++y)
        {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

How can I print out like a first one??

Thank you.

P/s: Sorry for my bad English.

Attachments star2.JPG 10.9 KB star1.JPG 11.32 KB

Also can someone show me the mechanism (or maybe the data structure, pseudocode, etc) of inner loop depend on outer loop. I still don't understand about the 'inner loop depend on outer loop' mechanisms. How the calculations behind that or logical step or etc.....

Why my images look messy?

Because we use CSS to align the images, it's not like a Word document. You'll need to organize the post so that it doesn't look goofy, much like you would in a web page. Note that we include a live preview underneath your post to facilitate proofreading.

I still don't understand about the 'inner loop depend on outer loop' mechanisms. How the calculations behind that or logical step or etc.....

I suspect what is meant is that the inner loop uses the counter from the outer loop:

#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 0; i < 5; i++) {
        for (j = 0; j <= i; j++) {
            putchar('*');
        }

        puts("");
    }

    return 0;
}

And to answer your first question, use two nested loops in sequence where the first prints spaces and the second prints stars:

#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= 5 - i; j++) {
            putchar(' ');
        }

        for (j = 1; j <= i; j++) {
            putchar('*');
        }

        puts("");
    }

    return 0;
}

HI... after practice and practice, I am now able to generate and also understand the concept nearly all the pyramid patterns by using looping (for loop)....
credit to:
-> @deceptikon
-> http://cprogrammingcodes.blogspot.com/p/pyramid.html
-> C Primer Plus 5th Edition (2004) (Publisher: SAMS)

:D

p/s: sorry for my bad ENglish.

Edited 3 Years Ago by lanzslumber

You missed the most amusing pattern (in my opinion):

#include <stdio.h>

void triangle(size_t n)
{
    for (size_t i = 0; i < n; i++) {
        for (size_t j = 0; j < n - i - 1; j++) {
            putchar(' ');
        }

        for (size_t j = 0; j <= i; ++j) {
            printf("%-2c", ~i & j ? ' ' : '1');
        }

        putchar('\n');
    }
}

int main(void)
{
    triangle(16);
    return 0;
}

Check this star triangle pattern program:
*
* *
* * *
* * * *
* * * * *
* * * * * *

#include<stdio.h>
#include<conio.h>

int main() {
    int i,j,rows;
    printf("Enter the number of rows\n");
    scanf("%d", &rows);

    for(i = 1; i <= rows; i++) {
    /* Prints one row of triangle */
        for(j = 1; j <= i; ++j) {
           printf("* ");
        }
        printf("\n");
    }
    getch();
    return 0;
}

Source : Triangle star pattern in C
You can find more pattern programs here C Pattern Programs
Hope it helps.

This article has been dead for over six months. Start a new discussion instead.