# Thread: Need help on Star Pattern using Loops

1. ## Need help on Star Pattern using Loops

2. You need to show us your attempt to solve the problem, by posting the code in code blocks, then we can help you. We cannot do the work for you. You can only learn to program in C by actually writing code yourself.

The same is true with anything you want to learn.

3. I have written this code for 1 no. pyramid which is working fine.. I just can't seem to figure out how to repeat it horizontally.
Code:
```#include<stdio.h>
int main()
{
int i, j, n;
printf("Enter No. of Rows : ");
scanf("%d", &n);

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

4. The first step is to make it output the same number of trailing spaces as leading spaces.

So that the pattern is essentially rectangular in shape.
....*....
...* *...
..* * *..
.* * * *.
* * * * *

Once you can do that, all you need to do is repeat the printing of each row.

5. Ok. I modified the code to print in rectangular shape. But i don't know how to repeat it row wise. Please help on that. Here is the modified code:-
Code:
```#include<stdio.h>
int main()
{
int n;
printf("Enter No. of Rows : ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
(i + j <= n) ? printf(" ") : printf(" *");
}
printf("\n");
}
return 0;
}```

6. No it doesn't.
Code:
```#include<stdio.h>
int main()
{
int n;
printf("Enter No. of Rows : ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
(i + j <= n) ? printf(".") : printf(" *");
}
printf("\n");
}
return 0;
}

\$ gcc bar.c
\$ ./a.out
Enter No. of Rows : 5
.... *
... * *
.. * * *
. * * * *
* * * * *```
All you did was push everything along one character.

You need to print the same number of trailing spaces as leading spaces on each row, so that the total number of characters on each row is the same.

Then you can repeat printing each row to end up with the same shape printed several times.

FWIW, your super compressed logic isn't doing you any favours.
Code:
```  for (int i = 1; i <= n; i++) {
int nSpaces = n - i;
int nStars = i;```
With that, you can write three simple loops to output
- the stars
- the trailing spaces.

One more thing, when you paste code, use "paste as text" and/or "copy as text".
Your IDE brings all the html markup with it and makes a mess when pasted here.

Code:
```/* main.cpp,  Draw Pyramid,  Created by Great on 9/10/22. */
#include <stdio.h>

#pragma mark - Interface
int add_array(char *, int, int, char);
int draw_pyramid(char *, int, char);

#pragma mark - Implementation
int add_array(char *buffer, int index, int number, char character) {

int n = number;
int i = index;

while (0 != n) {

n = n - 1;
buffer[i] = character;
i = 1 + i;
}

return i;
}
int draw_pyramid(char *buffer, int height, char character) {

int i = 0;
int star = 0;
int space = height;

while (1 != space) {

star = 1 + star;
space = space - 1;

i = add_array(buffer, i, space, ' ');
i = add_array(buffer, i, star, character);
i = add_array(buffer, i, star - 1, character);

i = add_array(buffer, i, space, ' ');
i = add_array(buffer, i, space, ' ');
i = add_array(buffer, i, star, character);
i = add_array(buffer, i, star - 1, character);

i = add_array(buffer, i, space, ' ');
i = add_array(buffer, i, space, ' ');
i = add_array(buffer, i, star, character);
i = add_array(buffer, i, star - 1, character);

i = add_array(buffer, i, space, ' ');
i = add_array(buffer, i, space, ' ');
i = add_array(buffer, i, star, character);
i = add_array(buffer, i, star - 1, character);

buffer[i] = '\r';
i = 1 + i;        /* 1 character for newline */
}

buffer[i - 1] = '\0'; /* Remove extra '\r' character */
return i;
}
int main(int argc, const char *argv[]) {

char buffer[1024];
int characters = draw_pyramid(buffer, 10, '*');

printf("%d characters \n", characters);
printf("%s\n", buffer);
return 0;
}```

8. Ty salem Got it now..

Code:
```#include<stdio.h>
int main()
{
int n,r;
printf("Enter No. of Rows : ");
scanf("%d",&n);
printf("Enter No. of Reptitions : ");
scanf("%d",&r);
for(int i=1;i<=n;i++)
{
for(int repeat=1;repeat<=r;repeat++)
{
for(int space=1;space<=n-i;space++)
{
printf(" ");
}
for(int star=1;star<=i;star++)
{
printf("* ");
}
for(int space=1;space<=n-i;space++)
{
printf(" ");
}
}
printf("\n");
}
return 0;
}```

9. Ty to u too kodax.. but i was trying to get it through loops only.. I'm just a newbie and haven't covered arrays yet..