Help with a Free Pascal program - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Help with a Free Pascal program

Posts: 2Member
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.

• Posts: 163Member
: 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:[code][color=Blue]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;
[/color][/code]
• Hollywood, FlPosts: 26Member
_Atex_, I just wanted to correct a small mistake in writing your power funtion. All [color=Red]pow[/color] should be [color=Green]power[/color].
[code]function [color=Green]power[/color](base:double;exponent:integer):double;
begin
if exponent<0 then [color=Red]pow[/color]:=1/[color=Red]pow[/color](base,-exponent)
else if exponent=0 then [color=Red]pow[/color]:=1
else [color=Red]pow[/color]:=base*[color=Red]pow[/color](base,pred(exponent));
end;
[/code]All Pascal compilers should catch this mistake.
• Hollywood, FlPosts: 26Member
I took _Atex_'s function routine and reformatted it.
[code]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;[/code]
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.
[code]{ 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.[/code]