0% found this document useful (0 votes)
122 views

Patterns C Programs

The document discusses how to write C programs to print various patterns using nested for loops. It explains printing patterns with increasing numbers of stars or numbers on each line. It then discusses more complex patterns that require multiple nested loops and printing spaces, stars, and numbers in a specific order to achieve the pattern. Several sample patterns are provided as examples for practice problems.

Uploaded by

Praveen Dhanush
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views

Patterns C Programs

The document discusses how to write C programs to print various patterns using nested for loops. It explains printing patterns with increasing numbers of stars or numbers on each line. It then discusses more complex patterns that require multiple nested loops and printing spaces, stars, and numbers in a specific order to achieve the pattern. Several sample patterns are provided as examples for practice problems.

Uploaded by

Praveen Dhanush
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

How to write pattern programs in C in a

few easy steps


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 well start by printing this simple pattern:
1.)

*
**
***
****

#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 well go with for loops. Things to notice in
this pattern:
1. No. of lines
2. Is the no. of stars increasing or decreasing per line and by how much?
And thats it. You can start coding. As you know youll 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:
2.)

*
**
***
****
*****
Theres 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 didnt believe me
and also she didnt 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
for j=0
print(a
}
for k=0
print(a
}
}

to n{
to n-i-1{
space)
to i{
star)

//limiting value of j is n-i-1


//limiting value of k is i

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
cant 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.
3.)

1
121
12321
1234321
12321
121
1
I wont lie but all my confidence flushed through the drain because this was difficult but I
couldnt tell that to her. Instead I told her its 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 shell

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
send the code for this pattern.

5.)
1**********1
12********21
123******321
1234****4321
12345**54321
123456654321
reply
help me to write this pattern..????
6.)

*
*#*
*#*#*
*#*#*#*#
*#*#*#*#*#
reply
I want below number pattern in c program.
7.)

1234
8765
9 10 11 12
16 15 14 13

You might also like