## Pascal

Moderators: None (Apply to moderate this forum)
Number of posts: 14004

This Forum Only

Help with a Free Pascal program Posted by mubblelyon on 2 Mar 2013 at 3:40 AM
Hi everyone,

I'm a beginner to programming, and would like to know, how would I go about making a program that displays all powers of 2 under 2000?

Using free pascal and lazarus.
Thanks.
Re: Help with a Free Pascal program Posted by _Atex_ on 2 Mar 2013 at 11:34 AM
: Hi everyone,
:
: I'm a beginner to programming, and would like to know, how would I
: go about making a program that displays all powers of 2 under 2000?
:
: Using free pascal and lazarus.
: Thanks.
:

Use this function to calculate powers:
```function power(base:double;exponent:integer):double;
begin
if exponent<0 then pow:=1/pow(base,-exponent)
else if exponent=0 then pow:=1
else pow:=base*pow(base,pred(exponent));
end;
```

Re: Help with a Free Pascal program Posted by quikcarlx on 6 Mar 2013 at 12:59 PM
_Atex_, I just wanted to correct a small mistake in writing your power funtion. All pow should be power.
```function power(base:double;exponent:integer):double;
begin
if exponent<0 then pow:=1/pow(base,-exponent)
else if exponent=0 then pow:=1
else pow:=base*pow(base,pred(exponent));
end;
```
All Pascal compilers should catch this mistake.
Re: Help with a Free Pascal program Posted by quikcarlx on 6 Mar 2013 at 1:16 PM
I took _Atex_'s function routine and reformatted it.
```function power ( base : double; exponent : integer ) : double;

begin
if exponent < 0 then
power := 1.0 / power( base, -exponent )
else
if exponent = 0 then
power := 1
else
power := base * power( base, pred( exponent ) )
end;```

But then I realized to be sure that it works correctly, that I need to write a program to check it out; so I did.
```{ power check for checking the base and exponent in a recursive routine

by Carl W. Skipworth  Rev Date 2013-Mar-3

}
program powercheck;

uses
Crt;

var
base : double;
exp  : integer;
value : double;

{\$include power.pas}

begin
writeln( 'Power number check for 2' );
base := 2.0;

for exp := -5 to 20 do begin
value := power( base, exp );
writeln( 'exp is', exp:4, ' value is', value:15:6 )
end;

end.```

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic