Code: #include<stdio.h> int main () { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) printf("%i",i); printf("\n"); } return 0; } The output is 1 22 333 4444 55555 this is the "for" method, how do i convert it into while and do..while format? i just still noob in programming , any expert help me out pls? thx
Code: int i=1,j=1; while(i<=5) { printf("%i",i); i++; while(j<=i) { printf("\n"); j++; } but if i write like this, i compile cant get the correct output same as the for loop 1 22 333 4444 55555
For while loop you should increment the counter as the last statement and then it will be equivalent. Do while loops are a bit different and cannot be equal to for loop but in your case applying the increment as the last statement of the loop would do.
Refer to my other post Code: for([COLOR=Red]i=1[/COLOR];[COLOR=Blue]i<=5[/COLOR];[COLOR=Magenta]i++[/COLOR]) { [COLOR=Lime] for(j=1;j<=i;j++) printf("%i",i); printf("\n");[/COLOR] } Becomes Code: [COLOR=Red]i=1[/COLOR]; while ( [COLOR=Blue]i<=5[/COLOR] ) { [COLOR=Lime] for(j=1;j<=i;j++) printf("%i",i); printf("\n");[/COLOR] [COLOR=Magenta]i++[/COLOR]; } Now apply the same transformation to the inner loop, and you're done.
If any one putted any silly problem by mistake then that post should be deleted because when we are searching such silly problem making the Image of this forum lower and user time is waisted.