I got an example task like those that might appear during the exams next week and I have certain trouble...
Quote:
program wykladniki;
uses crt;
var a:longint; n:longint; wynik:longint; x:longint; k:longint;
begin
clrscr;
writeln('Podaj podstawę potęgi:');
readln(a);
writeln('Podaj wykładnik:');
readln(n);
wynik:=1;
x:=a;
k:=n;
readkey;
repeat
begin
* if Odd(k) then
* begin
* wynik:= wynik * x;
* k:= k - 1;
* end
* else
* begin
* k:= k / 2 ;
* x:= x * x ;
* end;
writeln('' ,wynik);
end
until k>0;
end.
|
Odd(k) requires k to be of an integer type. On the other hand, k:= k / 2 requires the k to be of real type.
In theory I could try something with extra variables and stuff, but in this task I am limited to pre-defined variables with no space for this...
I ran outta ideas so far... got any?