Advertisement
CrazyDiver

Powers of 2

Dec 12th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 0.80 KB | None | 0 0
  1. procedure readnum(var inp: longint);
  2. begin
  3.   read(inp); //read num
  4. end;
  5. function pwr(in1,in2:longint):longint;
  6. var res,i:longint;
  7. begin
  8.   res:=1;  //firstly result eq 1
  9.   for i:=1 to in2 do  //repeat n2 times
  10.     res:=res*in1;  //res eq itself multiplie on in1
  11.   pwr:=res;  //assign to the function result res value
  12. end;
  13. procedure writePwrs(inp: longint);  //
  14. var
  15.   i: longint;
  16. begin
  17.   if inp <= 1 then  //if input value eq or less then inputted value then
  18.     write('0')  //write '0'
  19.   else  //else
  20.     for i := inp downto 1 do  //iterate all nums from inputted to 1
  21.       if i mod 2 = 0 then  //if present num is even then
  22.         write(' ', pwr(2, i));  //write space and 2^i
  23. end;
  24.  
  25.  
  26. var
  27.   inp: longint;
  28.  
  29. begin
  30.   readnum(inp);  //read num
  31.   writePwrs(inp);  //write powers of 2
  32. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement