If you are visiting this page then you must be having some problems in printing patterns. I remember I was in class 8 when we were taught nested loops and the first programs which we had to do using them were printing patterns. I always liked patterns till that day when this girl asked me to teach her *how to print patterns*. Now this was no ordinary girl, she was my first crush and I had to make an impression. She could never understand the usual way which I tried in the beginning. Back home I came up with this simple way of doing all basic patterns. Next day I was much more confident. I told her we’ll start by printing this simple pattern:

* ** *** ****

## #1: Understanding the loop concept:

The first question she asked what loop will we use? You see you can use both *for loops* and *while loops* but she always preferred *for loops*. So we’ll go with *for loops*. Things to notice in this pattern:

- No. of lines
- Is the no. of stars increasing or decreasing per line and by how much?

And that’s it. You can start coding. As you know you’ll need nested loops (*for loops* because she liked it). The first for loop will tell you about the *no. of lines* and the second will *print the stars*. One thing you must keep in mind that the limiting value of the *inner for loop* must depend on the *outer for loop*. In this case its *j<=i*. Okay so try to do it yourself and check it with the code.

//Pattern 1 #include<stdio.h> #include<conio.h> int main(){ int n,i,j; printf("Enter the no of lines: "); scanf("%d", &n); for(i=0;i<n;i++){ //number of lines for(j=0;j<=i;j++){ //number of stars in each line printf("*"); } printf("\n"); } getch(); return 0; }

## #2: Two inner loops to print a pattern:

Now this is what she told me- I could write this program by myself. Though I was heart broken, I regained composure and told her to print this pattern:

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

There’s an interesting thing about this pattern. It surely looks different because there are spaces before stars but other than that its same as the previous pattern. She didn’t believe me and also she didn’t have any idea of how to do it. Things to notice in this pattern: 1. No. of lines 2. No of triangles 3. Is the no. of stars increasing or decreasing per line and by how much? Now what are these triangles? You see there are 2 triangles in this pattern. One made my the spaces and another made by the stars.

So there will be 2 for loops inside the outer for loop. The first inner loop will print the spaces and the second one will print the stars. Also we must remember both the loops’ limiting value must depend on ‘i’. Now it may sound very confusing but lets see some pseudo code.

for i=0 to n{ for j=0 to n-i-1{ //limiting value of j is n-i-1 print(a space) } for k=0 to i{ //limiting value of k is i print(a star) } }

Now try to do it yourself and check it with the code.

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

Now this made her pretty happy. But there was one problem. The number of lines getting printed was one less than the number of lines asked to print. If you look carefully the number of lines getting printed are correct. Only 1 star per line is missing. For example in the first line, there is no star. In the second line there is 1 star. She managed to debug it herself. If you can’t then change the line 12 to

for(k=0;k<=i;k++){

## #3: A more complex pattern program:

Finally there was this pattern which was our homework and she told me to help her out.

1 121 12321 1234321 12321 121 1

I won’t lie but all my confidence flushed through the drain because this was difficult but I couldn’t tell that to her. Instead I told her it’s very easy. And we need to approach it the same way we did the pattern number 2. She asked me to help her out with the triangles and i drew this. Make sure you try to draw it first and check it with my one.

Yes there are 6 triangles but out of them only 3 are needed at a time. So we divide the pattern into two halves- *top and bottom*. And we do both of them separately. There will be *2 outer for loops* and there will be *3 nested for loops* inside each *outer for loop*. She told that she’ll write it herself and i told her to show me the pseudo code. With a little help she wrote and it was correct.

//1st outer for loop for i=0 to n{ for j=o to n-i-1{ print(a space) } for k=0 to (equal to) i{ print(k+1) } for l=i-1 to (equal to) 0{ //decrement l print(l+1) } } //2nd outer for loop for i=0 to n-2{ for j=0 to (equal to) i{ print(a space) } for k=0 to n-i-2{ print(k+1) } for l=n-i-2 to (equal to) 0{ //decrement l print(l+1) } }

Now try to do it yourself and check it with the code.

#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-1;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-1;i++){ for(j=0;j<=i;j++){ printf(" "); } for(k=0;k<n-i-2;k++){ printf("%d",k+1); } for(l=n-i-2;l>=0;l--){ printf("%d",l+1); } printf("\n"); } getch(); return 0; }

## #4: Some homework:

After the program compiled and ran properly she was so..uh excited that she gave me a hug. After I returned home I was thinking of her ( I actually thought she was the one). I was trying to find a way how to start a conversation with her the next day. Then I took out a piece of paper and wrote a few common patterns for her to practice:

question 1: n=3 ***** *** * *** ***** question 2: n=4 ******* *** *** ** ** * * ** ** *** *** ******* question 3: n=4 1 1 33 33 555 555 77777777 555 555 33 33 1 1

I managed to give it to her but I don’t think she ever tried these programs. Anyway you can always give it a try. Now I know there are 1001 better ways to do these patterns but for beginners these programs might help. You will find the answers to the questions at https://www.intechgrity.com/how-to-write-pattern-programs-in-c-in-a-few-easy-steps-answers/. 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.

shrutiIf we are printing * from left.firs t two loops wil be considered as a printing a space. Then when comes to k loop where the star wil be printing? Please reply for this

Nihal JoshiNo. Only two loops will be required. i and j. The outer loop will be i. And it will have two inner loops of j. One for ” ” and one for “*”

Hiren VasavaI want the pattern in c

1

22

333

4444

55555

ravifor(i=1;i<5;i++

{

for(j=1;j<=i;j++)

{

printf("%d",i);

}

}

abhishekhelp me out with this

abhishekk

bhishek

hishe

ish

s

karthiget your name in string and count the words and use “%number.f” in the printf statement it will print like that

sneha0

12

345

6789

explain me the code of this please

Mark AndersonHere is the code to print above pattern

for (i = 1; i <= rows; i++) {

for (j = 1; j <= i; j++) {

printf("%d ", count);

count++;

}

printf("\n");

}

Source : http://www.techcrashcourse.com/2016/02/print-natural-numbers-right-triangle-in-c.html

Hope it helps.

lalith kumar#include

void main()

{

int n,i,j,k=0;

printf(“enter the number of lines\n”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{

for(j=1;j<=i;j++,k++)

{

printf("%d",k);

}

printf("\n");

}

}

anchal sarrafint k=0;

for(int i=0;i<4;i++)

{

for(int j=0;j<=i;j++)

{

print(k);

k=k+1;

}

}

Adarsh1

33

555

7777

99999

Manish Kumarhelp me on this ******

*

*

*

sameereasy n nice explanation

lakshmii want

*

*

*

* * * * * * *

*

*

*

sunil srivstavait very simple

int n=1;

for (i=0;i<7;i++)

{

for(j=0;j<n;j++)

{

printf ("*");

if(i==3)

{

n=7;

}

else

{

n=1;

}

}

printf("\n");

}

i hope you like it

AdnanCan some one help me printing this.

************* (13 starts)

*********** (11 stars)

******** (8)

***** (5)

** (2)

saqibkhalid008if we nee to print this pattern then what will i do

*****

****

***

**

*

Nandan Kushwahafor(i=1;i=i;k–)

{

printf(“*”);

}

printf(“\n”);

}

Niranjan Pagadala1

33

555

7777

99999

Here is the code for above pattern###

#include

int main()

{

int n,i,j,k=1;

scanf(“%d”,&n);

for(i=1;i<=n;i++)

{

for(j=0;j<i;j++)

{

printf("%d",k);

}

k=k+2;

printf("\n");

}

}

fathui want

3

323

32123

323

3

even this code

4

434

43234

4321234

43234

434

4

Nareshi=13;

while(i>=2)

{

if(i==13)

{

for(j=1;j<=i;j++)

printf("*");

i-=2;

}

else

{

for(j=1;j<=i;j++)

printf("*");

i-=3;

}

printf("\n");

}

gabriellaI want this name pattern

Anna Rosemarie

Can you do it? What’s the code?

sumit rana********

*******

******

*****

****

***

**

*

how this pattern print?

Sonali PatilCan someone help me printing this

********

** **

********

kiruthuikapls write the program for this output:

0000110000

0001001000

0011111100

0100000010

1000000001. …..

have to print the letter A using binary patterns…

mahin bhuiyanplease give me this pattern solution Sample

Input Output

2

2

1 ********* *********

* * * *

* * * *

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

*********

* *

* *

********

anonymous#include

int main(){

int n,i,j,k,l;

printf(“Enter the no of lines: “);

scanf(“%d”,&n);

for(i=n;i>0;i–)

{

for(j=n-i;j>0;j–){

printf(” “);

}

for(k=(2*i)-1;k>0;k–){

printf(“*”);

}

printf(“\n”);

}

for(i=1;i<n;i++)

{

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

printf(" ");

}

for(k=0;k<(2*i)+1;k++){

printf("*");

}

printf("\n");

}

}

output:

n=3

*****

***

*

***

*****

anonymous#include

int main(){

int n,i,j,k,l;

printf(“Enter the no of lines: “);

scanf(“%d”,&n);

for(i=n;i>0;i–)

{

for(j=n-i;j>0;j–){

printf(” “);

}

for(k=(2*i)-1;k>0;k–){

printf(“*”);

}

printf(“\n”);

}

for(i=1;i<n;i++)

{

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

printf(" ");

}

for(k=0;k<(2*i)+1;k++){

printf("*");

}

printf("\n");

}

}

output :

Enter the no of lines: 3

*****

***

*

***

*****

——————————–

. .

ravi devani*

***

*****

*******

sakthidevisir i have some dut n-i not understand,tell me detailed explanation

ranjith marichamypls tell anyone phone number , i cant understand that star programme pls help me

Ravi kumarYou can check this tutorial on star pattern in C http://www.techcrashcourse.com/2016/02/c-program-to-print-triangle-pyramid-star-pattern.html . Hope it helps.

vivekQ. Draw a pattern for any natural number input ‘n’ :

n=1

*

n=2

* *

* *

*

n=3

* *

* *

** **

* *

*

Devendracan any one explain me how to print the following pattern:- Take input from user only for odd numbers i.e. n=3 or 5 or 7 …………..

n=5

***** *****

* *

* *

* *

***

*

*

*

*

n=3

*** ***

* *

***

*

*

Ankit12345

2345

345

45

5 solve this using nested while loop

dishai want this pyramid

* * * * *

* * * *

* * *

* *

*

what to do to get this output

desai dimpleI WANT:

A

BAB

CBABC

DCBABCD

Nanabielah* *

* *

*

* *

* *

Please help me to do this pattern in c programming.