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

Isoceles triangle

The Task

Draw an isosceles triangle.

The Program

/* Program: triangle3.c Using nested for loops to draw triangles facing both ways. */ #include <stdio.h> main() { int i, j ; for (i = 1 ; i <= 5 ; i++) { for (j = 1 ; j <= 5 +i ; j++) { if ( i > 5 - j) { printf("*") ; } else { printf(" ") ; } } printf("\n") ; } }

The Sample Run

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


tree

The Task

Draw a pine tree.

The Program

/* Program: tree.c Using nested for loops to draw a tree. */ #include <stdio.h> main() { int i, j, k ; for (k = 1 ; k <= 3; k++) { for (i = 1 ; i <= 5 ; i++) { for (j = 1 ; j <= 5 +i ; j++) { if ( i > 5 - j) { printf("*") ; } else { printf(" ") ; } } printf("\n") ; } } }

The Sample Run

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


Another tree

The Task

Draw a bigger pine tree.

The Program

/* Program: tree2.c Using nested for loops to draw another tree. */ #include <stdio.h> main() { int i, j, k ; for (k = 5 ; k <= 15; k += 5) { for (i = 1 ; i <= k ; i++) { for (j = 1 ; j <= 15 - k; j++) { printf(" ") ; } for (j = 1 ; j <= k +i ; ++j) { if (i > k - j) { printf("*") ; } else { printf(" ") ; } } printf("\n") ; } } }

The Sample Run

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


Multiplication Table

The Task

Print out a multiplication table

The Program

/* * File: timestab.c * ---------------- * Generates a multiplication table where each * axis runs from LowerLimit to UpperLimit. */ #include <stdio.h> /* * Constants * --------- * LOWER -- Starting value for the table * UPPER -- Final value for the table */ #define LOWER 1 #define UPPER 10 /* Main program */ main() { int i, j; for (i = LOWER; i <= UPPER; i++) { for (j = LOWER; j <= UPPER; j++) { printf(" %4d", i * j); } printf("\n"); } }

The Sample Run

1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100

The Lesson


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

Wednesday, 16-Sep-1998 18:18:12 EDT