Actually i am trying to create some patterns using loop like pascal's triangle etc. But the concept of loop is still not very clear to me as i am learning c language myself without any professional help. would anyone please elaborate me with the help of an example and explain me the reason of each line of code and its functions for better understanding.
THANK YOU............

Dani AI

Generated

Patterns such as Pascal's triangle are a good way to tie together loop structure, indexing and boundary checks. already covered the three parts of a for loop and nested loops; is right that deliberate practice helps; 's suggestion to read a basic C tutorial complements this. The example below builds each row from the previous one (O(n^2) work, O(n) extra space) and highlights the loop logic and common pitfalls.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int n = 10;                     // number of rows to generate
    int *prev = calloc(n, sizeof(int));
    int *curr = calloc(n, sizeof(int));
    prev[0] = 1;                    // row 0 = [1]

    for (int r = 0; r < n; ++r) {
        curr[0] = 1;                // left edge is always 1
        for (int c = 1; c < r; ++c) // interior entries: sum of two above
            curr[c] = prev[c - 1] + prev[c];
        if (r > 0) curr[r] = 1;     // right edge is 1 (for r >= 1)

        for (int c = 0; c <= r; ++c) // print entries for this row
            printf("%d ", curr[c]);
        printf("\n");

        int *tmp = prev; prev = curr; curr = tmp; // reuse buffers
    }

    free(prev);
    free(curr);
    return 0;
}

Notes and common pitfalls

  • prev[0] = 1 seeds the recurrence; the outer loop r creates one row per iteration.
  • Inner loop bounds (c = 1; c < r) are critical: off-by-one here produces wrong values or garbage.
  • Swapping pointers avoids copying the whole array each time and keeps memory at O(n).
  • Time is quadratic because every row r prints/creates ~r entries.
  • Watch integer overflow: binomial coefficients grow fast; use long long or big-integer libraries if n gets large.
  • For a triangular visual, prepend spaces when printing; for learning, walk through a small n on paper and trace prev and curr changes — that clarifies how the indices relate.

This complements the conceptual points already raised and gives a concrete pattern-focused example that isolates how loop bounds and indexing produce the triangle.

Recommended Answers

All 3 Replies

Actually i am trying to create some patterns using loop like pascal's triangle etc. But the concept of loop is still not very clear to me as i am learning c language myself without any professional help. would anyone please elaborate me with the help of an example and explain me the reason of each line of code and its functions for better understanding.
THANK YOU............

There are tons of online resources about the loops with examples and the C language as a whole, you might wanna check that out starting from here.(unless you are bounded by some rules or something like that).

You need to practice questions on loops , first try to solve basic problems patterns and then step up to high level, little practice will make you clear about the ideas of loops and you will visualize the functionality , working and code just by seeing the pattern.
Good luck..

Well, here's my crash course on loops:

Loops are things which helps you do repetitive jobs easily (but i think you knew that already, right?). So basically, say i wanna print your name 10 times; i do something like this:

for (int i = 0; i < 10; i++)
{
    printf ("deva89");
}

Now lets explain each part:
for is the type of loop (you have others, like: while, do-while, etc.)
(int i = 0; i < 10; i++) this can be broken up into three segments:
1. int i = 0; is the initialization of the loop variable (the variable that's going to keep track of the iterations.
2. i < 10; is the loop check. At each iteration the loop is going to check whether the condition holds; if yes it proceeds with the iteration, else it terminates.
3. i++ is incrementing your loop variable (in this case i). can you see why this is important? Also, this increment will happen after the loop body has been executed.
After that we have a curly brace to mark the beginning of the loop body.
printf ("deva89"); is what is inside the loop body.
Finally another curly brace ends the loop body.

Now go through the code once again and try to understand the format.
Now try to be the compiler yourself and give your code a mental run...something like:

first i is 0...is it less than 10...yes...so go inside loop body...what do we have here?...o..a printf...fine...print it...we have nothing else...so go back...increment the loop variable (remember, i said the increment part will happen after the loop body has been executed?)...so now i is 2...is it less than 10... ... ... i think you got it... (but don't cheat; do it a few more times; don't stop right now)

Now, if you have understood about the basic loop, lets go into nested loops (which you'll frequently encounter while playing with patterns)

Well, nested loops are loops inside loops...and, if you have followed the logic about a single loop you can easily extend that for nested loops.

Here's an example:

for (int i = 5; i > 0; i--)
{
    for (int j = i; j > 0; j--)
    {
        printf ("*");  // inside the inner(second) loop
    }
    printf ("\n");  // inside the outer(first) loop (see indentation)
}

Okay, take a deep breath...:)

Now, the i-- and j-- are similar to i++ (seen in the previous loop) only that they decrement instead of incrementing. (like, i starts with 5 and its greater than o, so goes inside and afterwards gets decremented by one...ie it becomes 4...and so on)

I'll leave you to figure out what this snippet is going to print...don't use an actual compiler to tell me that...use the strategy as earlier...be the compiler yourself and run it mentally...

let me know what you think...:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.