Patterns C Programs
Patterns C Programs
*
**
***
****
return 0;
}
*
**
***
****
*****
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)
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++){
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)
}
}
getch();
return 0;
}
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