Grade 10 · Data & calculations · Lesson and coding task · about 12 minutes
Open this task in the Delphi workspace →
Delphi has two kinds of division:
| Operator | Works on | Gives | Example |
|---|---|---|---|
| / | any numbers | Real answer | 7 / 2 = 3.5 |
| DIV | Integers only | whole part | 7 div 2 = 3 |
| MOD | Integers only | remainder | 7 mod 2 = 1 |
DIV and MOD are a power couple. "How many full minutes in 245 seconds, and what is left over?"
iMinutes := 245 div 60; // 4
iSeconds := 245 mod 60; // 5
The same idea splits money into notes and coins, learners into full buses and stragglers, and digits off a number — DIV/MOD questions appear in every NSC paper.
Trunc(iA / iB), you almost certainly wanted iA div iB. And remember DIV/MOD refuse to work on Real values.What you will do
- Read the total seconds from edtSeconds with StrToInt
- Use DIV to get the full minutes and MOD to get the seconds left over
- Show the answer on lblAnswer as: 4 min 5 sec
Hints
- There are 60 seconds in a minute, so DIV 60 and MOD 60.
- Build the caption with IntToStr: IntToStr(iMinutes) + ' min ' + IntToStr(iSeconds) + ' sec'
Model solution
procedure TfrmTime.btnConvertClick(Sender: TObject);
var
iTotal, iMinutes, iSeconds : Integer;
begin
iTotal := StrToInt(edtSeconds.Text);
iMinutes := iTotal div 60;
iSeconds := iTotal mod 60;
lblAnswer.Caption := IntToStr(iMinutes) + ' min ' + IntToStr(iSeconds) + ' sec';
end;