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]

_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.

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