There is such a program:

var n,c:integer; begin readln(n); while (n div 2 > 0) do begin c:=n mod 2; n:=n div 2; write(c,' '); end; write(1); end. 

I need to withdraw backwards. But better without using arrays.

  • and what does the program do? - Grundy
  • Converts from decimal to binary ... - goodalien

2 answers 2

Convert to binary representation is much easier:

 for с := 31 downto 0 do Write((n shr с) and 1); 

If you want to trim the extra zeros on the left, you can, for example:

 l := 0; repeat l := l + 1; if (n shr l) = 0 then Break; until l = 32; for c := l - 1 downto 0 do Write((n shr c) and 1); 
  • The second way is incorrect: you cannot use the loop variable after it, its value is undefined. - Pavel Mayorov
  • @PavelMayorov: It seems to be a classic loop in Pascal, and the value of the loop variable is guaranteed to be “correct” after the loop (that is, equal to the value at the last iteration, no matter how the loop ends). (But I'm not sure.) - VladD pm
  • @VladD not, there is unspecified behavior . EMNIP, Turbo and Borland Pascal do as you wrote, Delphi 7.0 leaves the following value (plus throws a warning). Well, it seems that the to and downto can behave differently. - Pavel Mayorov
  • @PavelMayorov: So, sacrificed beauty for efficiency, sorry. (I really didn’t work with the new versions after the 7th Turbo Pascal.) - VladD
  • @PavelMayorov, thanks, I didn’t know this feature (everything worked fine in fpc). The answer is corrected. While I was correcting, I found another jamb in the old version - with n = 0, nothing was displayed at all. - t3f

add c to Your line and output the Line after the cycle:

 var s:string; s = c + s;