Loops?...

I want to print following
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
using some loop as assignment, as I am a beginner programmer & frustrated as fail to write better code than below:


I am using following code
int space;
for(space=1; space<=6;space++)
{
cout<< ;
}
cout<<*<<
;

for(space=2; space<=6;space++)
{
cout<< ;
}
cout<<**<<
;
and
:
:
:
:
so on
But as code is so long so not acceptable
Thanks





Comments

  • : I want to print following
    : *
    : ***
    : *****
    : *******
    : *********
    : ***********
    : *********
    : *******
    : *****
    : ***
    : *
    : using some loop as assignment, as I am a beginner programmer & frustrated as fail to write better code than below:
    :
    :
    : I am using following code
    : int space;
    : for(space=1; space<=6;space++)
    : {
    : cout<< ;
    : }
    : cout<<*<<
    ;
    :
    : for(space=2; space<=6;space++)
    : {
    : cout<< ;
    : }
    : cout<<**<<
    ;
    : and
    : :
    : :
    : :
    : :
    : so on
    : But as code is so long so not acceptable
    : Thanks
    :
    :
    :
    :
    :
    :

    This will do the trick:

    [code]
    for(int i = 0; i < 6; ++i){
    int j = 0;
    for(; j < i; ++j){
    cout << ' ';
    }
    for(; j < 6; ++j){
    cout << '*';
    }
    cout << '
    '
    }
    [/code]

    Happy coding wishes
    the one and only
    [b]Niklas Ulvinge[/b] [white]aka [b]IDK[/b][/white]

  • : I want to print following
    : *
    : ***
    : *****
    : *******
    : *********
    : ***********
    : *********
    : *******
    : *****
    : ***
    : *
    : using some loop as assignment, as I am a beginner programmer & frustrated as fail to write better code than below:
    :
    :
    : I am using following code
    : int space;
    : for(space=1; space<=6;space++)
    : {
    : cout<< ;
    : }
    : cout<<*<<
    ;
    :
    : for(space=2; space<=6;space++)
    : {
    : cout<< ;
    : }
    : cout<<**<<
    ;
    : and
    : :
    : :
    : :
    : :
    : so on
    : But as code is so long so not acceptable
    : Thanks
    :
    :
    :
    :
    :
    [green]
    Try this and adapt it to what you want to do.
    [/green]
    [code]
    #include <stdio.h>
    #include

    int main()
    {
    int x, y, z;

    for (x=0; x<11; x++)
    {
    if(x & 1)
    {
    for (y=0; y<x; y++)
    printf("*");
    }

    printf("
    ");

    if(x==10)
    {
    for(y=11; y>0; y--)
    {
    if (y & 1)
    {
    for (z=0; z<y; z++)
    printf("*");
    }
    printf("
    ");
    }
    }
    }

    system("PAUSE");
    return 0;
    }
    [/code]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories