Pattern with loops
Hi, can anybody help me with this pattern using While, dowhile and For loop please
1 2 3
|
cout << "33333" <<endl;
cout << " 222 " << endl;
cout << " 1 " << endl;
|
Can anyone help me with this problem? Thanks in advance.
Can anyone help me with this problem? Thanks in advance. |
I don't see any problem yet.
How can I do that pattern using While loop, or Do While Loop or For Loop?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include <iostream>
#include <string>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
int i, n;
int N = 3;
int spaces;
int counter;
cout << "For loop : " << endl;
spaces = 0;
counter = N;
for(n = (N * 2) - 1; n >= 1; n -= 2)
{
for(i = 0; i < spaces; i++) cout << ' ';
for(i = 0; i < n; i++) cout << counter;
counter--; spaces++; cout << endl;
}
cout << endl;
cout << "While loop : " << endl;
spaces = 0;
counter = N;
n = (N * 2) - 1;
while(n >= 1)
{
for(i = 0; i < spaces; i++) cout << ' ';
for(i = 0; i < n; i++) cout << counter;
counter--; spaces++; cout << endl;
n -= 2;
}
cout << endl;
cout << "Do-While loop : " << endl;
spaces = 0;
counter = N;
n = (N * 2) - 1;
if(N > 0)
do
{
for(i = 0; i < spaces; i++) cout << ' ';
for(i = 0; i < n; i++) cout << counter;
counter--; spaces++; cout << endl;
n -= 2;
} while(n >= 1);
cout << endl;
cin.get();
return 0;
}
|
For loop :
33333
222
1
While loop :
33333
222
1
Do-While loop :
33333
222
1 |
Ok, thanks a lot.
Topic archived. No new replies allowed.