home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / LANGUAGS / MODULA2 / CMPPOWER.MOD < prev    next >
Text File  |  2000-06-30  |  745b  |  38 lines

  1. (* Raise integer to a positive power.  Repeat reading pairs
  2.    of integers, until you ecounter a 0.  Indicate invariant of loop.*)
  3.  
  4. MODULE power;
  5. FROM Terminal IMPORT WriteString, WriteLn;
  6. FROM MathLib0 IMPORT sqrt;
  7. FROM InOut    IMPORT ReadInt,WriteInt;
  8.  
  9. VAR a,b:INTEGER;
  10.  
  11. PROCEDURE power(x,n:INTEGER): INTEGER;
  12. VAR w,z,i: INTEGER;
  13.  
  14. BEGIN
  15.   w := x; i := n;
  16.   z := 1;
  17.   WHILE i # 0 DO
  18.     IF ODD(i) THEN z := z*w END;
  19.     w := w*w;
  20.     i := i DIV 2
  21.   END;
  22.   RETURN(z);
  23. END power;
  24.  
  25. BEGIN
  26.   WriteString('Enter number> ');
  27.   ReadInt(a);
  28.   WHILE a # 0 DO
  29.     WriteString('Enter power> ');
  30.     ReadInt(b);
  31.     WriteInt(a,4); WriteInt(b,4);
  32.     WriteInt(power(a,b),4);
  33.     WriteLn;
  34.     WriteString('Enter number> ');
  35.     ReadInt(a);
  36.   END;
  37. END power.
  38.