Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- program Arr2;
- {$APPTYPE CONSOLE}
- uses
- SysUtils;
- Type
- TArr = array of integer;
- procedure GetBin(var Num, Digit: Integer);
- begin
- Digit := Num mod 2;
- Num := Num div 2;
- end;
- function ReverseBin(Bin: TArr): TArr;
- var
- Index, Len: Integer;
- begin
- Len := Length(Bin);
- SetLength(Result, Len);
- for Index := (Len - 1) downto 0 do
- ReverseBin[Len - Index - 1] := Bin[Index];
- end;
- procedure Main();
- const
- CMaxNatural = 2147483647;
- CMinNatural = 1;
- var
- P, ArrSize, Num, Digit, Index: Integer;
- Bin: TArr;
- IsCorrect: Boolean;
- begin
- Write('Please, enter natural number to ', CMaxNatural,' : ');
- IsCorrect := false;
- repeat
- try
- Readln(P);
- if (P < CMinNatural) then
- Write('It is not a number from ', CMinNatural,' to ', CMaxNatural,' , please, enter valid number : ')
- else
- IsCorrect := true;
- except
- Write('Invalid input ! Please, enter natural number to ', CMaxNatural,' : ');
- end;
- until IsCorrect;
- Digit := 0;
- Num := P;
- ArrSize := 1;
- repeat
- GetBin(Num, Digit);
- SetLength(Bin, ArrSize);
- Bin[ArrSize-1] := Digit;
- inc(ArrSize);
- until (Num = 1);
- if (P > CMinNatural) then
- begin
- ArrSize := Length(Bin);
- inc(ArrSize);
- SetLength(Bin, ArrSize);
- Bin[ArrSize-1] := 1;
- Bin := ReverseBin(Bin);
- end;
- Write(P,' in binary digit system equals : ');
- for Index := 0 to high(Bin) do
- Write(Bin[Index]);
- Readln;
- end;
- var
- IsContinue, IsCorrect: Boolean;
- Answer: Char;
- begin
- Writeln('This program converts a natural number to binary digit system.');
- Main;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement