progs/fib.while
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 03 Nov 2013 00:41:24 +0000
changeset 178 d36363d648e3
parent 147 4725bba8ef26
child 182 9ce2414e470e
permissions -rw-r--r--
added
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
178
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
     1
/* Fibonnacci sequence implemented in 
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