It is possible to nest one for loop inside another. To demonstrate this structure, we'll concoct a program that will print the multiplication table:
included files
void main()
{
int cols,rows;
for(row=1;rows<13;rows++) /*outer loop*/
{
for(cols=1;cols<13;cols++) /*inner loop*/
printf("%3d",cols*rows);
printf("\n");
}
getch();
}
The output will be:
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8.....................................24
. . . ..................................... 36
. . . ..................................... 48
. . . ..................................... 60
. . . .......................................72
. . . .......................................84
. . . ........................................
12 24 36 48 60 72 84 96 108 120 132 144
If you have any question, you can ask me freely!
included files
void main()
{
int cols,rows;
for(row=1;rows<13;rows++) /*outer loop*/
{
for(cols=1;cols<13;cols++) /*inner loop*/
printf("%3d",cols*rows);
printf("\n");
}
getch();
}
The output will be:
1 2 3 4 5 6 7 8 9 10 11 12
2 4 6 8.....................................24
. . . ..................................... 36
. . . ..................................... 48
. . . ..................................... 60
. . . .......................................72
. . . .......................................84
. . . ........................................
12 24 36 48 60 72 84 96 108 120 132 144
If you have any question, you can ask me freely!
0 comments:
Post a Comment