How to write pattern programs in C in a few easy steps (Answers)

write-your-own-pattern-programs

write-your-own-pattern-programsThis post contains the answers to the questions in the post  http://www.intechgrity.com/understanding-and-writing-pattern-programs-in-ccpp/. I have worked out three different techniques to solve the pattern programs which are both simple and easy. It is advisable to check out all the three techniques to find out the one that is suitable for you. You can solve all the further problems using any of the techniques.

: Pattern 1

n=3
*****
 ***
  *
 ***
*****

: Answer

#include<stdio.h>
#include<conio.h>
int main(){
 int n,i,j,k,l;
 printf("Enter the no of lines: ");
 scanf("%d",&n);
 for(i=0;i<n;i++){
 for(j=0;j<=n-i-2;j++){
 printf(" ");
 }
 for(k=0;k<=i;k++){
 printf("%d",k+1);
 }
 for(l=i-1;l>=0;l--){
 printf("%d",l+1);
 }
 printf("\n");
 }
 for(i=0;i<=n-2;i++){
 for(j=0;j<=i;j++){
 printf(" ");
 }
 for(k=0;k<=n-i-2;k++){
 printf("%d",k+1);
 }
 for(l=n-i-3;l>=0;l--){
 printf("%d",l+1);
 }
 printf("\n");
 }
 getch();
 return 0;
}

: Pattern 2

n=4
*******
*** ***
**   **
*     *
**   **
*** ***
*******

: Answer

#include<stdio.h>
#include<conio.h>
int main(){
 int n,i,j,k,l;
 printf("Enter the no of lines: ");
 scanf("%d",&n);
 for (i = 1; i <= n; i++){
 for (j = n; j >= i; j--){
 printf("%d", j);
 }
 for (k = 0; k < 2 * (i - 1); k++){
 printf(" ");
 }
 for (l = i; l <= n; l++){
 printf("%d", l);
 }
 printf("\n");
 }
 for (i = n; i >= 1; i--){
 for (j = n; j >= i; j--){
 printf("%d", j);
 }
 for (k = 0; k < 2 * (i - 1); k++){
 printf(" ");
 }
 for (l = i; l <= n; l++){
 printf("%d",l);
 }
 printf("\n");
 }
 getch();
 return 0;
}

: Pattern 3

n=4
1      1
33    33
555  555
77777777
555  555
33    33
1      1

: Answer

#include<stdio.h>
 #include<conio.h>
 void func(int i, int j, int n){
 for (j = 0; j < i / 2 + 1; j++)
 printf("%d", i);
 for (j = 0; j < 2 * n - i - 1; j++)
 printf(" ");
 for (j = 0; j < i / 2 + 1; j++)
 printf("%d", i);
 printf("\n");
 }
 int main(){
 int n, i, j;
 printf("Enter the number of lines: ");
 scanf("%d", &n);
 for (i = 1; i <= 2 * n - 1; i = i + 2)
 func(i, j, n);
 for (i = 2 * n - 3; i >= 1; i = i - 2)
 func(i, j, n);
 getch();
 return 0;
 }

Hopefully you enjoyed reading this tutorial. If you have any doubts please use the comments.  Thank you and visit www.intechgrity.com for more tutorials.

16 comments

  1. Pingback: How to write pattern programs in C in a few easy steps

      • anam Reply

        I am beginner in c…and I don’t get the concept of pattern programming.. it is fine till two for loops(I,j) but the third and the fourth are really confusing…please help

  2. Pranav Reply

    Bro if u can explain me pattern 2 in which there are spaces and the value of j in easy steps

    • kush Reply

      use the solution to pattern 1on this page ,replace the ” ” and “*”

  3. suneel Reply

    can u guide me the below pattern

    * * * * * * *
    * * * * * *
    * * * *
    * *

    i did’t getting logic behind middle star b/w two triangles in first line

  4. hitesh810 Reply

    *****
    ***
    *
    ***
    *****

    This pattern program in 5 loops……. please check this and comment.
    #include

    int main()
    {
    int i,j,k,n;
    printf(“ENter your input”);
    scanf(“%d”,&n);
    for(i=1;i<=(2*n)-1;i++)
    {
    //j=2*n-1;
    if(i<=n)
    {
    for(k=1;k=2*i-1;j–)
    {
    printf(“*”);

    }

    }
    else
    {
    for(k=2*n-1;k>i;k–)
    {
    printf(” “);
    }
    for(j=(2*n)-1;j<=2*i-1;j++)
    {
    printf("*");
    }
    }

    printf("\n");

    }
    return 0;
    }

  5. Ekta singh Reply

    i need code for this question

    if n = 1
    *
    if n = 2
    *
    *
    *
    *
    *
    if n = 3
    *
    *
    **
    *
    *
    *
    **
    *
    *
    if n=4
    *
    *
    *
    **
    *
    **
    *
    *
    *
    **
    *
    **
    *
    *
    *
    *

    generalized code for this pattern

  6. Neela Hegde Reply

    How to print this pattern?
    ABCDEFGFEDCBA
    ABCDEFFEDCBA
    ABCDEEDCBA
    ABCDDCBA
    ABCCBA
    ABBA
    AA

Leave a Reply