freepascal - Convert fraction to decimal in Pascal -


i'm trying convert fraction decimal minimal number of repeating decimal places in brackets. should this:

for n=100, m=5 output should 20

for n=184, m=5 output should 36.8

for n=1, m=3 output should 0.(3)

for n=11, m=24 output should 0.458(3)

for n=100, m=7 output should 14.(285714)

my program failing last fraction (100/7). doing wrong? me?

this 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 write('.');   while remainder <> 0   begin     if remainder = (remainder*10 mod m) write('(');     write (remainder*10 div m);     if remainder = (remainder*10 mod m) begin        write(')');       break;      end; end;     remainder := remainder*10 mod m; end. 

the

 if remainder = (remainder*10 mod m) 

line checks 1 digit repeats. need check multiple lengths of repeating digits. 100/7 added exercise that.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -