Hello..... I want to ask you something..... My teacher gave us our final exam last October 16 & 17.... I want to ask you about this question: Create a program that could show this output
*
***
****
******

using while or for..... what I put in my answer sheet has only 1 while.... but my teacher told me there would be 3 while's in total how can that be???? Thanks for the help in advance.....:)

for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("*\n");
}
}

commented: Before postin code, try running it first to see if it works. This code is extremely wrong. -3

for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("*\n");
}
}

That doesn't do any good.

Create a program that could show this output
*
***
****
******

Are you sure about the output?

1st row should have 1 asterisk, 2nd row should have 3, 3rd should have 4 and 4th row should have 6?

for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("*\n");
}
}

wait i send you

for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("*\n");
}
}

It might work this time if the C compiler you are using grows sympathetic towards your blind persistence.

hiiiiii i'll give this qstn's answer in evening

There are a lot of ways you could produce that code using while loops or for loops, but most of them should not be coded using a while loop or for loop, unless your teacher is trying to make you use a math formula to calculate the number of '*' on each line. If I were you I'd just ask my teacher about this.

Hello..... I want to ask you something..... My teacher gave us our final exam last October 16 & 17.... I want to ask you about this question: Create a program that could show this output
*
***
****
******

If you'd try reading your post, you'd see that the question will never give you the proper answer. Your triangle does not give us the actual shape. Had you pressed PREVIEW you would have seen this. Wrap any lines you want to keep the proper formatting between CODE tags.

for(i=0;i<5;i++)
{
for(j=0;j<=i;j++)
{
printf("*\n");
}
}

Before posting code, format and test it first. You should never post working code as an answer to anyone's question (help them find the answer instead), and NEVER EVER post code that flat out doesn't work -- as your code doesn't. Twice.

There are a lot of ways you could produce that code using while loops or for loops, but most of them should not be coded using a while loop or for loop...

And what would you use? A switch statement? Don't just say something controversial (wrong), back it up with a reason. I for one always thought a loop is the absolute best statement to use to do a repetitive operation.

-------------------

Loop #1: Loop for each line.  Within this loop (nested is the term)
    Loop #2: Loop to print the spaces before  the *s (inner loop 1)
    Loop #3: Loop to print the *s (inner loop 2)

Base both the inner loops on the number of spaces and *'s needed for the specific line. This can be determined by a simple equation if you study the shape.

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

Here you go:

int main(){
    int stars=1;
    
    
    for(int a=0;a<2;a++)
    {
        stars++;
        for(int b=1;b<stars;b++)
        {
            cout<<"*";
        }
        cout<<endl;
        stars+=2;
        
        for(int b=1;b<stars;b++)
        {
            cout<<"*";
        }
        cout<<endl;
    
    }
    
    system("pause");

}

that was actually kind of fun, this is the kind of thing you're supposed to stress over. It comes hand in hand with programming. Well atleast while your learning it, once you get the hang of it it becomes.. well fun.


Oh come on mod, they took the test a month ago, they weren't able to figure it out in all that time they should be able to see the final code right?

IF your formatting is off, edit your post using the code tags like the moderator said.

The reason I'd code it w/o a loop is because it would take less time to hard code a few lines of '*' rather than spend time to think about the pattern then write loops based on it. The code to write the loops alone is probably longer than the code to just print the '*'s' out. And the code definitely makes more sense at first glance hard coded than if it is put in for loops. Also, you might want to consider that the sentence 'nested is the term' only makes sense to Yoda. Learning you are.

The reason I'd code it w/o a loop is because it would take less time to hard code a few lines of '*' rather than spend time to think about the pattern then write loops based on it.

man, the whole point is to get the concept down. It is simple enough to change the four lines of *'s into 20 lines, if your using a loop. If you use a bunch of cout's you're going to have a hell of a time doing any repetitive action anymore times then you'd see in a hypothetical situation. Besides that's not what the teacher asked for making hard coding it completely pointless.

The reason I'd code it w/o a loop is because it would take less time to hard code a few lines of '*' rather than spend time to think about the pattern then write loops based on it.

You've obviously never passed a programming course.

Ok... I've checked my test paper and got the question here it is: Create a program that could give an output of a chrismas tree starting from the first * to the 4th line of *
I counted it....to every line it adds 2 to every next line it goes... I asked my teacher how may loops do I have to make and he gave us a hint 3.... but the problem is when I get to the 2nd and 3rd loop I can't really figure it out.... and that was a written exam so I have to make it without the help of a computer but this was my answer in my test paper....

while(a<16)
{
a=a+2
a++
}

So your output is this:

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

No of stars: 1,3,5,7.

You can use the logic provided by WaltP for this:

Loop #1: Loop for each line.  Within this loop (nested is the term)
Loop #2: Loop to print the spaces before  the *s (inner loop 1)
Loop #3: Loop to print the *s (inner loop 2)[/code]

To elaborate on that:

Loop #1: Since there are 4 rows, use a for loop that iterates 4 times.

for(i=1; i<=ROWS; i++)

Loop #2: Print the spaces first. First row needs 3 spaces before printing a *, 2nd row needs 2, 3rd row needs 1 and 4th needs none. Arrive at a loop condition which iterates according to the current ROWS value.

Loop #3: Again arrive at another loop condition which prints out the appropriate no of *'s based on the current ROWS value.

Your code structure:

for(i=1; i<=ROWS; i++) //Outer loop for no. of rows
{
       for(condition)
           printf(" ");  //print spaces in inner loop
       for(condition)
           printf("*");  //printf *'s in inner loop
       printf("\n");  //after printing stars go to newline
}

I saw people asking this, in Yahoo Answers last week.

I like this funnier leaning tree version(I don't have a life to be writing this stuff):

for(int i = 0; i < 16; i++)
    {
         for(int j = 0; j < 16 - i; j++)
         {
                 for(int l = 0; l < 1 + j / 2; l++)
                 {
                         printf(" ");
                 }
         }
         for(int k = 0; k < i; k++)
         {
                 printf("*");
         }
         printf("\n");
    }

It's also possible to do an upside down version, with only two 'for' loops, and one 'if-else'.
It should be easy to turn that into a tree, too. A good practice-exercise.

I saw people asking this, in Yahoo Answers last week.

I like this funnier leaning tree version(I don't have a life to be writing this stuff):

for(int i = 0; i < 16; i++)
    {
         for(int j = 0; j < 16 - i; j++)
         {
                 for(int l = 0; l < 1 + j / 2; l++)
                 {
                         printf(" ");
                 }
         }
         for(int k = 0; k < i; k++)
         {
                 printf("*");
         }
         printf("\n");
    }

It's also possible to do an upside down version, with only two 'for' loops, and one 'if-else'.
It should be easy to turn that into a tree, too. A good practice-exercise.

oh yeah that was pretty funny!

was that intentional or was it just a mess up?

i've actually done the tree before, it might even still be on this computer..

int counter=0; // the counter is used to make sure we are writing to the right space in the array.. you'll see.
    int space=0; // declares how many spaces we start out with.
    int star=7; // this is the declaratio of how many stars we have
    // here we are declaring all our variables.

for(int a=0;a<4;a++){ // for loop used for each line and the counters.
        for(int b=0;b<space;b++){
                cout<<' ';
                counter++;
        } 
        for(int b=counter;b<star;b++){
                cout<<'*'; 
                counter++;
                }  
        space++; 
        star= star-2;
        counter=0; 
    }

This was originally for an array, i just did a quick edit on it so there might have been some mistakes, but the logic should be fine.

i made it upside :P that's not giving away the code.. right?

i made it upside :P that's not giving away the code.. right?

Debatable... :icon_wink:

You've obviously never passed a programming course.

You've obviously never had friends.

You've obviously never had friends.

Ahh, when someone points out why your advice is lacking, you go to personal attack. I've been soundly trounced! :icon_rolleyes:

Actually, it was you who went on a personal attack with your above comment about never passing a programming course. If you aren't prepared to take it, don't dish it out.

Actually, it was you who went on a personal attack with your above comment about never passing a programming course. If you aren't prepared to take it, don't dish it out.

yeah but his had a point, yours was just kind of childish. Most beginning computer programming classes seem to stress that these loops are only used to a minimal effect but they can but used on a larger scale. It's just that there's no point in doing a larger scale when your still learning it.

Seeing as you didn't seem to grasp that, him jumping to the conclusion that you have never finished a programming class made a lot of sense.

Now can this topic die? The poster seems to have gotten their answer.

His post was still a personal attack - a personal attack is judged by intent, regardless of whether there's "meant to be a lesson" in it. I agree with you that my comment was unnecessary and I wish I could withdraw it. However, WaltP should consider that haughtiness in any form is a personality flaw. Also, my original post was pointing out that in the real world, nobody would look at the code and attempt to apply a pattern if they knew they only needed that specific series of *'s. I assumed that someone else would give the kid the pattern or that he would do the logical thing and ask his teacher. Maybe its my own fault, but that's how I interpreted Walt's attitude. Consider this issue done.

Sorry it took me a long time to reply because I have other business to attend to and it really got me famished.... By the way thanks a lot I got the problem solved and thanks for the advice and teachings.... I will write another problem next time...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.