# Exercise - Use built-in functions

Completed

You're a developer at CRONUS International Ltd. and want to test your newly acquired knowledge about built-in functions. You want to create a page that calculates Armstrong numbers (to 10000) and displays them in a message box.

An Armstrong number is calculated by taking each digit to the power of the final digit and creating a sum of the results.

Example: 153

1 raised to power 3 = 1

5 raised to power 3 = 125

3 raised to power 3 = 27

Sum of 1, 125, and 27 = 153

Therefore, 153 is an Armstrong number.

Note

For the purpose of this exercise, we are not using the definition of the official Armstrong numbers. The official definition is that the power is the number of digits, not the final digit. If you look for Armstrong numbers online, you will find a different definition and solution.

• Create a new page.

• Create the OnOpenPage trigger.

• Define variables for the Armstrong calculation.

• Write the calculation logic.

• Show the result in a message box.

## Steps

1. Select File > New File and save this file by selecting File > Save. Name the file ArmstrongCard.Page.al.

2. Create a new page in this file by using code snippets. Enter tpage and press the Tab key.

3. Change the ID to 50113 and the name to Armstrong Card.

4. Set the PageType property to Card and set the UsageCategory property to Documents.

5. Remove the SourceTable property and set the Caption property to Armstrong Card.

6. Create the OnOpenPage trigger under the myInt definition. You can use the ttrigger snippet.

7. Remove the default myInt variable.

8. Create the following local variables in the OnOpenPage trigger:

• Counter1 Integer

• CounterText Text[5]

• Counter2 Integer

• PowerNumber Integer

• Number Integer

• Result Integer

• ResultList List of [Integer]

• ArmstrongNumbers Text

9. Put the following code between the begin and end block of the OnOpenPage.

Code Example

    Ch10 := 10;
Ch13 := 13;
Newline := Format(Ch10) + Format(Ch13);
for Counter1 := 0 to 10000 do begin
CounterText := Format(Counter1);
Evaluate(PowerNumber, CopyStr(CounterText, StrLen(CounterText), 1));
for Counter2 := 1 to StrLen(CounterText) do begin
Evaluate(Number, CopyStr(CounterText, Counter2, 1));
Result += Power(Number, PowerNumber);
end;

if Result = Counter1 then
Clear(Result);
end;

foreach Counter1 in ResultList do
ArmstrongNumbers += Newline + Format(Counter1);

Message(ArmstrongNumbers);

10. Remove the Actions from the page.

11. Remove the group from the content area.

12. The page should now resemble:

page 50113 "Armstrong Card"
{
PageType = Card;
ApplicationArea = All;
UsageCategory = Documents;
Caption = 'Armstrong Card';

layout
{
area(Content)
{

}
}

trigger OnOpenPage()
var
Counter1, Counter2, PowerNumber, Number, Result : Integer;
CounterText: Text[5];
ArmstrongNumbers, Newline : Text;
ResultList: List of [Integer];
Ch10, Ch13 : Char;
begin
Ch10 := 10;
Ch13 := 13;
Newline := Format(Ch10) + Format(Ch13);
for Counter1 := 0 to 10000 do begin
CounterText := Format(Counter1);
Evaluate(PowerNumber, CopyStr(CounterText, StrLen(CounterText), 1));
for Counter2 := 1 to StrLen(CounterText) do begin
Evaluate(Number, CopyStr(CounterText, Counter2, 1));
Result += Power(Number, PowerNumber);
end;

if Result = Counter1 then
Clear(Result);
end;

foreach Counter1 in ResultList do
ArmstrongNumbers += Newline + Format(Counter1);

Message(ArmstrongNumbers);
end;
}

13. Open the launch.json file in the .vscode folder. Set the startupObjectId setting to 50113 and the startupObjectType setting to Page.

14. Publish your extension to the sandbox. Select View > Command Palette... or use the Ctrl+Shift+P shortcut keys.

15. Enter AL: Publish in the search box (or press the F5 key) and then select the command from the list.

16. Verify that the Microsoft Dynamics 365 Business Central application launches and that the Armstrong Card page appears.

17. Verify that the message box shows four numbers: 1, 153, 1634, 9474.