English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm supposed to make this:

*
**
***
**** <---With this line being the input number by the user.
***
**
*

How do I do this using for loops in C++?

something like...

int main()
{
int n;
int k;

cout<<"Enter n:";
cin>>n;

for(k=1; k<=n; k++) <---Is this right
{
for(what goes here
{
for(what goes here
{
cout<<"*"<<" "; \<---Is this right?
}
}
}
cout< return 0;
}
Am I missing anything else?

2007-02-05 08:19:40 · 3 answers · asked by ? 2 in Computers & Internet Programming & Design

It didn't format right on this webpage its supposed to be:

*
**
***
****
*****
******
|*****
||****
|||***
||||**
|||||*

Where | = space

2007-02-05 09:17:41 · update #1

3 answers

#include "stdafx.h"
#include

using namespace std;

int main()
{

int n,k,l,m;
cout<<"Enter n:";
cin>>n;

for(k=1; k<=n; k++) // You were right
{
for(l = 1; l <= k; l++)
{
cout<<"*"<<" "; // the extra <<" " puts a space, for the line to look as you have it in the example, use cout <<"*";
}
cout << endl;
} //End increment
for(k=1; k <= n; k++) // You were right
{
for(l = 1; l <= k; l++)
{
cout<<" "<<" "; // the extra <<" " puts a space, for the line to look as you have it in the example, use cout <<"*";
}
for(m = 1; m <= (n-k); m++)
{
cout<<"*"<<" ";
}

cout << endl;
} //End decrement
cin >> n; // Remove, this is just to see the pattern at the end
return 0;
} // End main

Of course, there are other ways to skin a cat

2007-02-05 08:28:04 · answer #1 · answered by Tarie N 3 · 0 0

int main()
{
int n,k,i;
cout<<"Enter n:";
cin>>n;
for(k=1; k<=n; k++) //<---Is this right
{for(i=1;i++<=k;) //what goes here
{cout<<"* "; // \<---Is this right?
}
cout< }
for(k=n; --k) //<---Is this right
{for(i=k;i++<=n) //what goes here
{cout<<" "; // \<---Is this right?
}
{for(i=k;i++ {cout<<"* "; // \<---Is this right?
}
cout< }
return 0;
} // I dunno, it has been a long time.
// Yahoo suppresses the indentation.

2007-02-05 18:35:14 · answer #2 · answered by J C 5 · 0 0

check out http://www.pscode.com for great sample codes.

2007-02-05 16:49:31 · answer #3 · answered by Richard H 7 · 0 2

fedest.com, questions and answers