do while number triangle

im so dumb cat figure this out, guyz can you help me up..

i want a code like this

if you:

input: 1
1
input: 2
22
1
input: 3
333
22
1
input: 4
4444
333
22
1
input: 5
55555
4444
333
22
1

but i have no idea how.. can you please help me i need to code this badly i cant think straight anymore
1
2
3
4
5
6
7
8
9
10
int input_number;
cin >> input_number;
for(int i = input_number; i < 0; i--)
{
     for(int j = 0; j < input_number; j++)
     {
               cout<<i;
      }
     cout<<"\n";
}

hi there HiteshVaghani1 hmmm i was wondering about do while loop, but i have no idea how it work i used this code

// alkarloz.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <conio.h>

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
int x, y = 5;

cout<<"ENTER A NUMBER: ";
cin>> x;
do

{
cout<<y <<y <<y <<y <<y<<endl;
y--;
while (y>=x)
{
cout<<y <<y <<y <<y <<endl;
y--;
while (y>=x)
{
cout <<y <<y <<y<<endl;
y--;
while (y>=x)
{
cout<<y <<y <<endl;
y--;
while (y>=x)
{
cout<<y <<endl;
y--;
}}}}}while (y>=x);
system ("PAUSE");
getch ();
}


but i got is its in reverse :

input: 1
55555
4444
333
22
1
input: 2
4444
333
22
1
and so on. i want to reverse this.
i want is:

input: 1
1
input: 2
22
1
input: 3
333
22
1
input: 4
4444
333
22
1
input: 5
55555
4444
333
22
1

been in circles im all stressout.. :(
You're almost there, just reverse your loop logic a bit.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;

int main( int argc, char **argv )
{
   int num;

   cout << "Enter a number: ";
   cin >> num;

   // You might want to add code to validate the number here

   do
   {
      for( int i = 0; i < num; i++ )
         cout << num;

      cout << endl;
   }  while ( num-- );

   return 0;
}
hi hmm.. is there a way i can do this using do while loop only?.. i want to use do while loop only on this project but i dont know how to do it, all ive got is the reverse one, ive been trying to manipulate the code for more than 5 hours now. and im all stress out.. can you give me a hint?.. or explain the logic regarding it.. thanks a bunch if you may..
Not really just a do-while loop, no. At least not in a sensible way I can think of.

You could use a do-while with another while inside of it, replacing my for loop with a while. Or you could do a do-while with a switch or if statement inside of it.

If you're using functions you could use some sort of recursive method, I guess. It might hurt your head a bit, though.

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
#include <iostream>

using namespace std;

void RecursivePrint( int num, int count )
{
   if( !count )
      cout << "\n";
   else
   {
      cout << num;
      RecursivePrint( num, --count );
   }
}

int main( int argc, char **argv )
{
   int num;

   cout << "Enter a number: ";
   cin >> num;

   // You might want to add code to validate the number here

   do
   {
      RecursivePrint( num, num );
   }  while ( num-- );

   return 0;
}
hi iHutch thank you so much i figured it out already thanks for first sample i just put some 3 variables and one is validation and so on... thank you very much.. until nexttime :) ill wait 24hours before i closed this thread.
hey alkarloz sorry for misunderstanding you...
Last edited on
Topic archived. No new replies allowed.