Help with a Free Pascal program

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.

Comments

  • : 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]
  • quikcarlxquikcarlx Hollywood, Fl
    _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.
  • quikcarlxquikcarlx Hollywood, Fl
    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]
Sign In or Register to comment.

Howdy, Stranger!

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

Categories