0

Hello,

I am trying to write this program in Delphi that calculates the amount of change required to make a certain amount.

The user enter a number between 1 and 200. The program then calculates the amount of 100, 50, 20, 10, 5, 2, 1 bils required.

But there is a problem it doesnt always works.

When I enter the number '123' it says I need 1 - '100', 1 - '20', 1 - '2' and 1 - '1' dolar bills.

but when I enter it says I need 1 - '100', 1 - '2' and 1 - '2' dollar bils. it should say that I need 2 - '2' dollar bils

I think its something wrong with my algorithm where I am not checking the values correctly

Here is my code:

```
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin,
Vcl.Buttons;
type
TForm1 = class(TForm)
Label1: TLabel;
sedAmount: TSpinEdit;
Button1: TButton;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
lblR100: TLabel;
lblR50: TLabel;
lblR20: TLabel;
lblR10: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
lblR5: TLabel;
lblR2: TLabel;
lblR1: TLabel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure UpdateLabels();
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
iAmount: integer;
iLeftOver: integer;
iR100, iR50, iR20, iR10, iR5, iR2, iR1: integer;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
lblR100.Caption := '0';
lblR50.Caption := '0';
lblR20.Caption := '0';
lblR10.Caption := '0';
lblR5.Caption := '0';
lblR2.Caption := '0';
lblR1.Caption := '0';
end;
procedure TForm1.UpdateLabels();
begin
lblR100.Caption := IntToStr(iR100);
lblR50.Caption := IntToStr(iR50);
lblR20.Caption := IntToStr(iR20);
lblR10.Caption := IntToStr(iR10);
lblR5.Caption := IntToStr(iR5);
lblR2.Caption := IntToStr(iR2);
lblR1.Caption := IntToStr(iR1);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
iR100 := 0;
iR50 := 0;
iR20 := 0;
iR10 := 0;
iR5 := 0;
iR2 := 0;
iR1 := 0;
iAmount := StrToInt(sedAmount.Text);
{ Now for the complex calculations... }
if(iAmount >= 100) then
begin
iLeftOver := iAmount mod 100;
iR100 := iR100 + 1;
if(iLeftOver >= 100) then
begin
iLeftOver := iLeftOver mod 100;
iR100 := iR100 + 1;
end;
if(iLeftOver >= 50) then
begin
iLeftOver := iLeftOver mod 50;
iR50 := iR50 + 1;
end;
if (iLeftOver >= 20) then
begin
iLeftOver := iLeftOver mod 20;
iR20 := iR20 + 1;
end;
if(iLeftOver >= 10) then
begin
iLeftOver := iLeftOver mod 10;
iR10 := iR10 + 1;
end;
if(iLeftOver >= 5) then
begin
iLeftOver := iLeftOver mod 5;
iR5 := iR5 + 1;
end;
if(iLeftOver >= 2) then
begin
iLeftOver := iLeftOver mod 2;
iR2 := iR2 + 1;
end;
if(iLeftOver >= 1) then
begin
iLeftOver := iLeftOver mod 1;
iR1 := iR1 + 1;
end;
UpdateLabels();
end;
{r100}
end;
end.
```

Please help me fix this problem.

*Edited 2 Years Ago by MasterHacker110*