progs/fib.while
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Wed, 13 Nov 2013 20:07:26 +0000
changeset 189 04346d82fe01
parent 182 9ce2414e470e
child 207 f824e1331fc6
permissions -rw-r--r--
added cw1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 178
diff changeset
     1
/* Fibonnacci numbers implemented in 
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
     2
   the WHILE language */
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
     3
   
147
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     4
write "Input a number ";
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     5
read n;
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
     6
x := 0;   // start values
147
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     7
y := 1;
80
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 76
diff changeset
     8
while n > 0 do {
147
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
     9
  temp := y;
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    10
  y := x + y;
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    11
  x := temp;
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
    12
  n := n - 1  // decrement counter
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
    13
};
147
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    14
write "Result ";
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 93
diff changeset
    15
write y
75
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 73
diff changeset
    16