Advertisement
Samatter

Multiples.pas

Nov 19th, 2022 (edited)
1,384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.46 KB | None | 0 0
  1. program Multiples;
  2. begin
  3.   while (true) do begin
  4.     var NUM, ERR, LEN, CHR: Integer;
  5.     var MAX, IND, SUM: LongInt;
  6.     var STR: String;
  7.     var OKE: Boolean;
  8.     // read max
  9.     write('Enter MAX number: ');
  10.     readln(STR);
  11.     val(STR, MAX, ERR);
  12.     if (ERR <> 0) then begin
  13.       write('Error! ');
  14.       continue;
  15.     end;
  16.     // read num
  17.     write('Enter NUM number: ');
  18.     readln(STR);
  19.     val(STR, NUM, ERR);
  20.     if (ERR <> 0) then begin
  21.       write('Error! ');
  22.       continue;
  23.     end;
  24.     // check
  25.     if (MAX > 0) and (NUM > 1) then begin
  26.       writeln('Working...');
  27.       // set sum and file
  28.       SUM := 0;
  29.       var OUT: TextFile;
  30.       AssignFile(OUT, MAX + '-' + NUM + '.txt');
  31.       rewrite(OUT);
  32.       // begin loop
  33.       for IND := 1 to MAX do begin
  34.         STR := IntToStr(IND);
  35.         LEN := Length(STR);
  36.         // length num or more
  37.         if (LEN >= NUM) then begin
  38.           OKE := TRUE;
  39.           // check last num symbols
  40.           for CHR := 1 to (NUM-1) do if (STR[LEN-CHR] <> STR[LEN]) then OKE := FALSE;
  41.           // check one mone symbol
  42.           if (LEN > NUM) and (STR[LEN-NUM] = STR[LEN]) then OKE := FALSE;
  43.           // nice
  44.           if OKE then begin
  45.             writeln(OUT, STR);
  46.             SUM := SUM + 1;
  47.           end;
  48.         end;
  49.       end;
  50.       // close
  51.       CloseFile(OUT);
  52.       write('Done! SUM = ', SUM, '. Press ENTER.');
  53.       readln;
  54.     end else write('Error! ');
  55.   end;
  56. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement