Apply for Zend Framework Certification Training

c

< Pattern using loop in c Last >




1. Hollow Diamond Pattern

Output for n = 5
    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

Logic: Print leading spaces, then print stars only at the boundaries of the diamond.

#include <stdio.h>

int main() {
    int n = 5, i, j;

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++)
            printf(" ");

        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    for (i = n - 1; i >= 1; i--) {
        for (j = 1; j <= n - i; j++)
            printf(" ");

        for (j = 1; j <= 2 * i - 1; j++) {
            if (j == 1 || j == 2 * i - 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}

 

2. Butterfly Pattern
*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *

Solution in C

#include <stdio.h>

int main() {
    int n = 5, i, j;

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++)
            printf("*");

        for (j = 1; j <= 2 * (n - i); j++)
            printf(" ");

        for (j = 1; j <= i; j++)
            printf("*");

        printf("\n");
    }

    for (i = n - 1; i >= 1; i--) {
        for (j = 1; j <= i; j++)
            printf("*");

        for (j = 1; j <= 2 * (n - i); j++)
            printf(" ");

        for (j = 1; j <= i; j++)
            printf("*");

        printf("\n");
    }

    return 0;
}

 

Interview concept: Nested loops, symmetry, and calculating the number of spaces.
3. Palindromic Number Pyramid
        1
      2 1 2
    3 2 1 2 3
  4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5

Solution in C

#include <stdio.h>

int main() {
    int n = 5, i, j;

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n - i; j++)
            printf("  ");

        for (j = i; j >= 1; j--)
            printf("%d ", j);

        for (j = 2; j <= i; j++)
            printf("%d ", j);

        printf("\n");
    }

    return 0;
}

Logic: Print numbers in descending order to 1, then ascending order from 2 to the current row number.
4. Pascal's Triangle
        1
      1   1
    1   2   1
  1   3   3   1
1   4   6   4   1

Solution in C

#include <stdio.h>

int main() {
    int n = 5, i, j;
    long long value;

    for (i = 0; i < n; i++) {
        for (j = 0; j < n - i - 1; j++)
            printf("  ");

        value = 1;

        for (j = 0; j <= i; j++) {
            printf("%4lld", value);
            value = value * (i - j) / (j + 1);
        }

        printf("\n");
    }

    return 0;
}


Logic: Each row starts with 1. Calculate each next value using the previous value and the binomial coefficient relationship.
5. Concentric Square Number Pattern
For n = 4:
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4

Solution in C

#include <stdio.h>

int main() {
    int n = 4, i, j, min;

    for (i = 0; i < 2 * n - 1; i++) {
        for (j = 0; j < 2 * n - 1; j++) {
            min = i;

            if (j < min)
                min = j;
            if (2 * n - 2 - i < min)
                min = 2 * n - 2 - i;
            if (2 * n - 2 - j < min)
                min = 2 * n - 2 - j;

            printf("%d ", n - min);
        }
        printf("\n");
    }

    return 0;
}


Logic: Each position is assigned a number based on its minimum distance from the four boundaries.
6. X-Shaped Star Pattern
*       *
  *   *
    *
  *   *
*       *

Solution in C

#include <stdio.h>

int main() {
    int n = 5, i, j;

    for (i = 0; i < n; i++) {
        for (j = 0; j < n; j++) {
            if (i == j || i + j == n - 1)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}

 

Logic: Print a star when either diagonal condition is true:
- Main diagonal: i == j
- Secondary diagonal: i + j == n - 1
Note: This version uses five columns, so the output is compact. The number of spaces between the stars is determined by the diagonal positions.
7. Hourglass Star Pattern
*********
  *******
    *****
      ***
        *
      ***
    *****
  *******
*********

Solution in C

#include <stdio.h>

int main() {
    int n = 5, i, j;

    for (i = n; i >= 1; i--) {
        for (j = 0; j < n - i; j++)
            printf("  ");

        for (j = 1; j <= 2 * i - 1; j++)
            printf("* ");

        printf("\n");
    }

    for (i = 2; i <= n; i++) {
        for (j = 0; j < n - i; j++)
            printf("  ");

        for (j = 1; j <= 2 * i - 1; j++)
            printf("* ");

        printf("\n");
    }

    return 0;
}

 

Logic: The upper half decreases from 2*n-1 stars to one. The lower half increases from three stars to 2*n-1.
8. Binary Triangle Pattern
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1

Solution in C

#include <stdio.h>

int main() {
    int n = 5, i, j;

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            if ((i + j) % 2 == 0)
                printf("1 ");
            else
                printf("0 ");
        }
        printf("\n");
    }

    return 0;
}

 

Logic: Use the parity of i + j to alternate between 1 and 0.
9. Hollow Butterfly Pattern
*        *
**      **
* *    * *
*  *  *  *
*   **   *
*   **   *
*  *  *  *
* *    * *
**      **
*        *

Solution in C

#include <stdio.h>

int main() {
    int n = 5, i, j;

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            if (j == 1 || j == i)
                printf("*");
            else
                printf(" ");
        }

        for (j = 1; j <= 2 * (n - i); j++)
            printf(" ");

        for (j = 1; j <= i; j++) {
            if (j == 1 || j == i)
                printf("*");
            else
                printf(" ");
        }

        printf("\n");
    }

    for (i = n; i >= 1; i--) {
        for (j = 1; j <= i; j++) {
            if (j == 1 || j == i)
                printf("*");
            else
                printf(" ");
        }

        for (j = 1; j <= 2 * (n - i); j++)
            printf(" ");

        for (j = 1; j <= i; j++) {
            if (j == 1 || j == i)
                printf("*");
            else
                printf(" ");
        }

        printf("\n");
    }

    return 0;
}

Logic: Print stars only at the left and right boundaries of each wing, leaving the inside hollow.
Note: This implementation repeats the middle row. To print a perfectly symmetric 9-row butterfly for n = 5, start the second loop at i = n - 1.

< Pattern using loop in c Last >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top