Advertisement
pseudocreator

Sum of reciprocal up to n

Mar 19th, 2014
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MODULE z;
  2. FROM RealInOut IMPORT WriteReal;
  3. FROM InOut IMPORT WriteString, WriteLn, WriteCard, ReadCard;
  4. CONST
  5.     dgr = 1;
  6.     ggr = 1000;
  7. TYPE
  8.     INTERVAL = [dgr..ggr];
  9. VAR
  10.    sum : REAL;
  11.    ok : BOOLEAN;
  12.    n : CARDINAL;
  13.  
  14. PROCEDURE SOR(n: CARDINAL; VAR ok: BOOLEAN): REAL;
  15. VAR
  16.    sum : REAL;
  17.    i : INTERVAL;
  18. BEGIN
  19.      IF (n > dgr) AND (n < ggr) THEN
  20.        ok := TRUE;
  21.        sum := 1.0;
  22.        FOR i := 2 TO n DO
  23.           sum := sum + (1.0/FLOAT(i))
  24.        END;
  25.        RETURN sum
  26.      ELSE
  27.          ok := FALSE;
  28.          RETURN 0.0
  29.      END;
  30. END SOR;
  31.  
  32. BEGIN
  33.      WriteString('Enter the range, n: ');
  34.      ReadCard(n);
  35.      WriteLn;
  36.      sum := SOR(n,ok);
  37.      IF ok THEN
  38.        WriteString('Sum of reciprocal up to');
  39.        WriteCard(n,0);
  40.        WriteString(' is: ');
  41.        WriteReal(sum,0)
  42.      ELSE
  43.        WriteString('n is not in RANGE, modifies it in CONST!');
  44.        WriteLn
  45.      END
  46. END z.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement