# How to you loop this?

This is a discussion on How to you loop this? within the C++ Programming forums, part of the General Programming Boards category; 1 12 123 1234 12345 1234 123 12 1 or 1 12 123 1234 12345 123456 1234567 123456789 can some1 ...

1. ## How to you loop this?

1
12
123
1234
12345
1234
123
12
1

or

1
12
123
1234
12345
123456
1234567
123456789

can some1 found me some example so i can have some idea..

2. I did the first example using two for loops. However it come out
123454321 and not

1
2
3
4
5
4
3
2
1 but you can just format that.

Code:
```#include <iostream>
#include <conio.h> // for getch

using namespace std;

int main ()
{
int i;

for (i = 1; i<= 5; i++) // loop to get 12345
{
cout <<i;
}
for (i = 4; i>=1; i--) // loop to get 4321
{
cout <<i;
}
getch(); //ignore this for my compiler
return 0;
}```
IM just not exactly sure how to get 1
12
123
But this is a start i guess

Run a search on creating triangles. Your question is nearly as common as the "clear the screen" question.

-Skipper

4. Here a small example using printf:
Code:
```#include <string.h>

int main(void)
{
int i;
char msg[] = "12345";
int l = strlen(msg);

for(i = 1; i < l; i++) printf("%.*s\n", i, msg);
for(i = l; i > 0; i--) printf("%.*s\n", i, msg);
return 0;
}```

5. ## hmm..

hmm.. i know how to get the first half of the first problem

Code:
```for (a = 2; a < 7; a++)
{
for (x = 1; x < a; x++)
{
cout << x;
}
cout << endl;
}```
i know there is a way to write the entire problem 1 in nested loops.. i just did it not but a week ago.. but it seems to have fled my brain.. bummer...

this set of code produces the following output
Code:
```1
12
123
1234
12345```

6. Originally posted by Monster
Here a small example using printf:
Code:
```#include <string.h>

int main(void)
{
int i;
char msg[] = "12345";
int l = strlen(msg);

for(i = 1; i < l; i++) printf("%.*s\n", i, msg);
for(i = l; i > 0; i--) printf("%.*s\n", i, msg);
return 0;
}```

Hey Monster my teacher never teach us printf. Is there other command that is equal to printf? I think printf is Java's command but we learning C++

7. printf() is valid in C as well as C++. (here is no cout in C.) Check out sprintf() too.

thz

thz

Here is an example of the while

while (statement) // if this is true perform statement_1 if false skip it
{
statement_1;
}

i hope this helped
cj