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();
}
#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?
#include <iostream>
usingnamespace 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++.
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
#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();
}