number pyramid

Nov 17, 2012 at 11:20am
Hi everyone, i need urgent help about this nested loop program

need an output like this

1 2 3 4
4 5 6
6 7
7

so far i am done with the two loops outer one and for the blanks
but unable to uderstand the inner loop for each line that is printing 1-4 in first line and than next lune is starting with the last digit of the previous line. HELP!!!

#include <stdio.h>
#include <conio.h>
main()
{
int outer, inner,b;
for(outer=1;outer<=4;outer++)
{
for(b=1;b<outer;b++){ //for the spaces
printf(" ");
}
for (inner=1 ) // need helpp ahead
}
getch();
}
Nov 17, 2012 at 11:33am
Hi, asra !

This example will help you :
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
#include <stdio.h>
#include <conio.h>
main()
{
int outer; 
int inner = 4;
int nPyramid = 1; 
int b;

for(outer=1;outer<=4;outer++)
{

for(b=0;b< inner;b++){ //for the spaces
printf("%d ",nPyramid);
nPyramid = nPyramid + 1; //Increase the pyramid value by 1
}

nPyramid = nPyramid - 1; //Remove this line and see the effect
printf("\n"); //Next line

inner = inner -1;

}
getch();
}


Where the code that you don't understand? Any question?
Nov 17, 2012 at 11:56am
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
#include <iostream>


using namespace std;

int main()
{
int outer;
int inner = 4;
int nPyramid = 1;
int b;

for(outer=1;outer<=4;outer++)
{

for(b=0;b<inner;b++)
{                   //for the spaces
cout<<nPyramid;

nPyramid++;         //Increase the pyramid value by 1
}

nPyramid--; //Remove this line and see the effect
cout<<endl; //Next line

inner--;

}

}


The same program above.
I just changed some C thigns in C++.
Nov 17, 2012 at 4:56pm
Thankyou jackson and skarla, its working but not showing the spaces on the left side... can you help me on that as well? oh damn !!! my question isnt showing spaces on the left side...
this is the real one


1     2     3     4
      4     5     6
            6     7
                  7


apologies!! :)
Last edited on Nov 17, 2012 at 5:03pm
Nov 17, 2012 at 5:14pm
hey guys, i have found the way :P (oh Lord! thanks am not feeling dumb :D )
thank you thank you both of you

here is what i did to your program

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

#include <stdio.h>
#include <conio.h>
main()
{
int outer; 
int inner = 4;
int nPyramid = 1; 
int b,a;

for(outer=1;outer<=4;outer++)
{

for(b=1;b<outer;b++){
           printf("  ");
           }
           for(a=0;a<inner;a++)
           {
           
           
printf("%d ",nPyramid);
nPyramid = nPyramid + 1; //Increase the pyramid value by 1
}

nPyramid = nPyramid - 1; //Remove this line and see the effect
printf("\n"); //Next line

inner = inner -1;

}
getch();
}


and got this


1     2     3     4
      4     5     6
            6     7
                  7

Topic archived. No new replies allowed.