Friday, September 9, 2016

Decimal To Roman Numeral Converter in Delphi

In this article I would like to share with you a sample program that I wrote using Delphi version 6 as my programming language that will ask the user to give a number and then our program will convert the given decimal number by our user into roman numerals. 

The code is very short and easy to understand in Delphi.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com.
 
My mobile number here in the Philippines is 09173084360
.



Borland Delphi 6.0






Sample Program Output


Program Listing

unit romans;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Edit1: TEdit;
    Button1: TButton;
    Label5: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function DecToRoman(Decimal: Longint): string;
const
  Numbers: array[1..13] of Integer =
    (1, 4, 5, 9, 10, 40, 50, 90, 100,
    400, 500, 900, 1000);
  Romans: array[1..13] of string =
    ('I', 'IV', 'V', 'IX', 'X', 'XL',
    'L', 'XC', 'C', 'CD', 'D', 'CM', 'M');
var
  i: Integer;
begin
  Result := '';
  for i := 13 downto 1 do
    while (Decimal >= Numbers[i]) do
    begin
      Decimal := Decimal - Numbers[i];
      Result  := Result + Romans[i];
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label5.Caption:= 'The equivalent value of ' + Edit1.Text
      + ' to Roman Numberal is ' + DecToRoman(StrToInt(Edit1.Text))+'.';
 end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Edit1.text :='';
Label5.Caption := '';
Edit1.SetFocus
end;

end.

No comments:

Post a Comment