Difference between revisions of "CSC111 Exercises on nested for-loops"

From dftwiki3
Jump to: navigation, search
(Created page with "--~~~~ ---- =Exercise 1= This is taken from Section 4.7 in the textbook: <source lang="python"> for i in range(4): for j in range( i+1 ): print( "*", end="" ) p...")
 
(Exercise 1)
Line 3: Line 3:
  
 
=Exercise 1=
 
=Exercise 1=
 
+
<br />
 
This is taken from Section 4.7 in the textbook:
 
This is taken from Section 4.7 in the textbook:
 
+
<br />
 
<source lang="python">
 
<source lang="python">
 
for i in range(4):
 
for i in range(4):
Line 13: Line 13:
  
 
</source>
 
</source>
 
+
<br />
 
The output is
 
The output is
 
+
<br />
 
<source lang="text">
 
<source lang="text">
 
  *
 
  *
Line 22: Line 22:
 
  ****
 
  ****
 
</source>
 
</source>
 
+
<br />
 
;Question
 
;Question
 
: Modify the program so that it outputs this pattern
 
: Modify the program so that it outputs this pattern
 
+
<br />
 
<source lang="text">
 
<source lang="text">
 
  ****
 
  ****
Line 33: Line 33:
 
</source>
 
</source>
 
<br />
 
<br />
 +
 
=Exercise 2=
 
=Exercise 2=
 
<br />
 
<br />

Revision as of 10:33, 24 February 2014

--D. Thiebaut (talk) 10:32, 24 February 2014 (EST)


Exercise 1


This is taken from Section 4.7 in the textbook:

for i in range(4):
   for j in range( i+1 ):
      print( "*", end="" )
   print()


The output is

 *
 **
 ***
 ****


Question
Modify the program so that it outputs this pattern


 ****
 ***
 **
 *


Exercise 2


Modify the program so that it outputs this pattern:

    *
   **
  *** 
 ****