0

I am trying to input Z: 20202020 X: 202020 C: 2020

But after that, my terminal doesn't show anything. Why is that? I have tried with some other input and it works just fine.

Edit: I realized that the same problem occurs whenever the X input is more than 5 digit.

program PowerTower;
Uses Math;
var
x,
z: real;
c: int64;
hasil : real;

function pembulatan(o: real): int64;
begin
pembulatan:= Round(o);
end;


function dopower(h,y: real): 
real;
begin
  dopower:= power(h,y)
end;


function ayam(a, b: real) : real;
begin
  begin
    if (b=0) then
      ayam := 1
    else
      ayam:= dopower(a,ayam(a, b-1));
  end;
end;

begin
readln(z);
readln(x);
readln(c);
hasil:= ayam(z, x);

writeln(pembulatan(hasil) mod c);
readln;
end. 
Ken White
  • 123,280
  • 14
  • 225
  • 444
Adeva1217
  • 13
  • 3
  • 1
    You get a [stack overflow](https://en.wikipedia.org/wiki/Stack_overflow) due to too deep [recursion](https://en.wikipedia.org/wiki/Recursion_(computer_science)). – Andreas Rejbrand Feb 25 '21 at 13:56
  • Is there any way to fix it? Or my program simply incapable of doing more than 5 digit input on variable x – Adeva1217 Feb 25 '21 at 14:57
  • You can use iteration instead of recursion (at least as long as no other issues appear, like floating-point overflow). – Andreas Rejbrand Feb 25 '21 at 15:04
  • What @AndreasRejbrand says about iteration is quite correct. Every recursive calculation has at least one iterative equivalent. See f.i. https://stackoverflow.com/questions/159590/way-to-go-from-recursion-to-iteration. Btw you *still* have not told us which Pascal implementation you are using. Telling us that would help us know whether we can reproduce the problems you are having. – MartynA Feb 25 '21 at 15:25
  • 2
    I'm not sure if I'm reading it correctly but `ayam(a, b)` seems to compute `a` to the power of `b!` (`b` factorial). Is that what you intend? Also be careful about doing exact check for equality when using `real` numbers (like your `if (b=0)`). – lurker Feb 26 '21 at 02:48
  • 1
    Definitely lurker is correct. When you input bigger numbers the "resolution" of the number decreases, up to the point that subtracting 1 does not go to a precise zero, and your recursion goes forever. You could check for "<= 0", that would work if you don't need negative numbers. Last note: please don't use parentheses in pascal IFs, it is not C! – linuxfan says Reinstate Monica Feb 26 '21 at 11:27
  • 1
    @linuxfansaysReinstateMonica "please don't use parentheses in pascal IFs" Why do you say that? Pascal is fine with parenthesized IFs ... and they help beginners avoid tripping over operator precedence ... – MartynA Feb 26 '21 at 19:57
  • @MartynA, well, I was referring only to unnecessary ones, like "if (b=0) then ...", which BTW is the only IF found in the OP code. – linuxfan says Reinstate Monica Mar 01 '21 at 08:41
  • @linuxfansaysReinstateMonica: Oh, ok, thanks. I was only asking because I *think* the OP's `if ()` came from my answer to a previous q of his. – MartynA Mar 01 '21 at 09:29

0 Answers0