/*
Program: box.c
Using nested for loops to draw a box.
*/
#include
main()
{
int i, j ;
for (i = 1 ; i <= 5 ; ++i )
{
for (j = 1 ; j <= 5 ; ++j)
{
printf("*") ;
}
printf("\n") ;
}
}
The Sample Run
*****
*****
*****
*****
*****
A different box
The Task
Draw a 9x5 box of *'s.
The Program
/*
Program: box2.c
Using nested for loops to draw another box.
*/
#include
main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 9 ; j++)
{
printf("*") ;
}
printf("\n") ;
}
}
The Sample Run
*********
*********
*********
*********
*********
Diagonal
The Task
Draw a 5x5 box of *'s but with O's on the diagonal.
The Program
/*
Program: diag.c
Using nested for loops to draw
a diagonal of O's.
*/
#include
main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 5 ; j++)
{
if (i == j)
{
printf("O") ;
}
else
{
printf("*") ;
}
}
printf("\n") ;
}
}
The Sample Run
O****
*O***
**O**
***O*
****O
Triangle
The Task
Draw a triangle of *'s.
The Program
/*
Program: triangle.c
Using nested for loops to draw a triangle.
*/
#include
main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= i ; j++)
{
printf("*") ;
}
printf("\n") ;
}
}
The Sample Run
*
**
***
****
*****
Another triangle
The Task
Flip the triangle.
The Program
/*
Program: triangle2.c
Using nested for loops to draw
a triangle facing the other way.
*/
#include
main()
{
int i, j ;
for (i = 1 ; i <= 5 ; i++ )
{
for (j = 1 ; j <= 5 ; j++)
{
if ( i > 5 - j)
{
printf("*") ;
}
else
{
printf(" ") ;
}
}
printf("\n") ;
}
}