UMBC CS 201, Fall 98
UMBC CMSC 201 & 201H Fall '98 CSEE | 201 | 201 F'98 | lectures | news | help

box

The Task

Draw a 5x5 box of *'s.

The Program

/* Program: box.c Using nested for loops to draw a box. */ #include <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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 <stdio.h> 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") ; } }

The Sample Run

* ** *** **** *****


CSEE | 201 | 201 F'98 | lectures | news | help

Wednesday, 16-Sep-1998 18:20:47 EDT