for Loop

+1 Islam Kamilov · April 1, 2015
Hello!
I need a help to creating a star pattern with for loops... It should seems like 
if n=5;
the output is:
*
***
*****
***
*

Post a Reply

Replies

Oldest  Newest  Rating
+1 Dol Lod · April 2, 2015
Split this into 2 for loops and one main body.

For loop that goes from 1 to N, but not including N.

Print out N stars

For loop that goes down from N up to 1 not including N. 
0 Islam Kamilov · April 2, 2015
Can you fix my program?
/*The output is: 
Please Enter The Height Of The Pattern You Want:
5
*
**
***
**
*


*/
/*  but I need the output is: 

if height is 5;

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


*/
#include <stdio.h>

int main() {
    int height;
    printf("Please Enter The Height Of The Pattern You Want: \n");
    scanf("%d", &height);
    
    for (int row = 1; row<=height; row++) {
        
        for (int col = 0; col<row; col++) {
            if (col > (height-row)) {
                printf(" ");
            }
            else {
                printf("* ");
            }
        }
        printf("\n");
    }
    
    return 0;
}
+1 Dol Lod · April 2, 2015
I told you the general approach. I am not going to code this for you. 

Split this into two loops.

Have the first loop print the first part in ascending order. ex. if height is 5, make it print *\n***\n,***\n
Then have the second loop print in descending order. ex. if height is 5, make it print ***\n*\n.

That's really all you need. 

If the height is even you were unclear about what you do. There is absolutely no reason to do this in one for loop instead of 2.
+1 Clark Alaan · April 12, 2015
Islam,

The best way to learn programming is by doing it. It's been quite a while since this topic was posted but I just want to point out that there is a tutorial for this in Bucky's series for C. Tutorial 28 - How to make a table talks about nested for loops which is what Arjun is trying to tell you. Not trying to be rude, but I'm aiming to only state facts. Hopefully this is somewhat helpful.
0 Islam Kamilov · April 16, 2015
Than you so much, Guys!!!
  • 1

C

107,252 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator