Exercise 4.4 Solutions

  1.        *
          ***
         *****
        *******
       *********
      ***********
     *************
  2. var width : int
    
    put "Enter the maximum width"
    get width
    
    % do top half of triangle
    for row : 1 .. width
       for col : 1 .. row
          put "*" ..
       end for
       put ""
    end for 
    
    % do bottom half of triangle
    for decreasing row : width - 1 .. 1
       for col : 1 .. row
          put "*" ..
       end for
       put ""
    end for 
    1. var n : int
      
      put "Enter a positive integer"
      get n
      
      loop
         exit when n = 0
         put n mod 10
         n := n div 10
      end loop 
    2. var n, temp, pow : int
      
      get n
      temp := n
      pow := 1
      
      loop
         exit when temp <= 9
         pow := pow * 10
         temp := temp div 10
      end loop
      
      loop
         exit when n = 0
         put n div pow
         n := n mod pow
         pow := pow div 10
      end loop 
  3. %note: there are several different approaches you could take to this question, this is only one
    for decreasing i : 5 .. 1
       for decreasing j : 5 .. 1
          if j <= i then
             put j..
          else
             put " "..
          end if
       end for
       put ""
    end for