C++ Nested Loop

hi there, i specify other Looping codes but i am hardly to make an output like this.


Output should be like this (Ctrl+F9)

1
12
123
1234
12345


I'm not sure if i used \n for the new printf.

i need it on Wednesday but i guess i may fail to submit earlier, but if you can help me right away pls asap. thank you

Codes for While Loop:

#include<stdio.h>
#include<conio.h>
int main()
{
int i;
clrscr();
while (i<=5;)
{
printf("%i\n" ,i);
i++;
}
getche();
return(0);
}

Codes for Nested Loop:

#include<stdio.h>
#include<conio.h>
int main()
{
int i, j;
clrscr();
for (i=1; i<=3; i++)
for (j=j; j<=5; j++)
printf("%i\t %i\n" , i,j);
getche();
return(0);
}
closed account (28poGNh0)
if you using code::blocks compilator this code could help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include <stdio.h>
# include <conio.h>

int main()
{
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=i;j++)
            printf("%d",j);
        printf("\n");
    }

    return(0);
}
i'll try it on http://cpp.sh/

there was a problem!

2:20: fatal error: conio.h: No such file or directory compilation terminated.

pls help me.. :(
remove line 2.
closed account (28poGNh0)
Ops I accedntly put this library in the code Do as @fcantoro suggests
closed account (1CfG1hU5)
corrected code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<stdio.h>
//#include<conio.h>  // won't compile on cpp.sh, but ok with some compilers
int main()
{
int i;  // seems to work ok.  i would declare int i = 0; to make sure starts at 0
//clrscr(); // also won't compile on cpp.sh, part of conio.h
while (i<=5) // while (i<=5;), cannot have a semi-colon inside of while loop parentheses
{
printf("%i\n" ,i);
i++;
}
//getche(); // good function, won't compile on cpp.sh, okay with some compilers
            // no need to use.  not waiting for a menu.  no screen to pause.
return(0);
}

Last edited on
closed account (1CfG1hU5)
nested loop code

1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
//#include<conio.h> // won't compile on cpp.sh.  good header
int main()
{
int i, j;
//clrscr(); // good function, won't compile, not supported on cpp.sh web site
for (i=1; i<=3; i++)
for (j=j; j<=5; j++)  // j is starting at 0 when run on cpp.sh web.  
printf("%i\t %i\n" , i,j);
//getche();  // won't compile on cpp.sh.  doesn't have the header conio.h
return(0);
}

Last edited on
closed account (1CfG1hU5)
didn't fix number paddings yet
closed account (1CfG1hU5)
this works good techno

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# include <stdio.h>
//# include <conio.h> // this won't run on cpp.sh web site.  

int main()
{
    for(int i=1;i<=5;i++)
    {
        for(int j=1;j<=i;j++)
            printf("%d",j);
        printf("\n");
    }

    return(0);
}

Last edited on
Topic archived. No new replies allowed.