aprilduck - 1 year ago 269

Pascal Question

I'm trying to convert fraction to decimal with minimal number of repeating decimal places in brackets. It should look like this:

for

`n=100, m=5`

`20`

for

`n=184, m=5`

`36.8`

for

`n=1, m=3`

`0.(3)`

for

`n=11, m=24`

`0.458(3)`

for

`n=100, m=7`

`14.(285714)`

My program is failing for the last fraction (100/7). What am I doing wrong? Could someone help me?

This is my code:

`Program fraction2decimal(output);`

var n,m,remainder: integer;

begin

read(n,m);

d := 0;

write (n div m);

remainder := n mod m;

if remainder <> 0 then write('.');

while remainder <> 0 do

begin

if remainder = (remainder*10 mod m) then write('(');

write (remainder*10 div m);

if remainder = (remainder*10 mod m) then begin

write(')');

break;

end;

end;

remainder := remainder*10 mod m;

end.

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

The

```
if remainder = (remainder*10 mod m)
```

line only checks for one digit repeats. You need to check for multiple lengths of repeating digits. Obviously the 100/7 was added to the exercise for just that.