Post Reply 
Roman Numeral Function
01-17-2016, 09:29 PM (This post was last modified: 01-17-2016 10:51 PM by smp.)
Post: #1
Roman Numeral Function
Here's a simple function I created. Get a Roman Numeral as output for an integer input. Invoke by RN(integer).

Code:

EXPORT RN(N)
BEGIN
  LOCAL B:="";
  WHILE N>=1000 DO
    B:=B+"M";
    N:=N-1000;
  END;
  IF N>=900 THEN
    B:=B+"CM";
    N:=N-900;
  END;
  IF N>=500 THEN
    B:=B+"D";
    N:=N-500;
  END;
  IF N>=400 THEN
    B:=B+"CD";
    N:=N-400;
  END;
  WHILE N>=100 DO
    B:=B+"C";
    N:=N-100;
  END;
  IF N>=90 THEN
    B:=B+"XC";
    N:=N-90;
  END;
  IF N>=50 THEN
    B:=B+"L";
    N:=N-50;
  END;
  IF N>=40 THEN
    B:=B+"XL";
    N:=N-40;
  END;
  WHILE N>=10 DO
    B:=B+"X";
    N:=N-10;
  END;
  IF N=9 THEN
    B:=B+"IX";
    N:=N-9;
  END;
  IF N>=5 THEN
    B:=B+"V";
    N:=N-5;
  END;
  IF N=4 THEN
    B:=B+"IV";
    N:=N-4;
  END;
  WHILE N>=1 DO
    B:=B+"I";
    N:=N-1;
  END;
  RETURN B;
END;

This is pretty simple stuff, but I'm still learning PPL.

UPDATE: I messed up and posted code that did not handle 40 properly. I've updated with the correct code.

smp
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Roman Numeral Function - smp - 01-17-2016 09:29 PM
RE: Roman Numeral Function - John Colvin - 01-19-2016, 09:46 PM
RE: Roman Numeral Function - smp - 01-19-2016, 10:12 PM
RE: Roman Numeral Function - Gerald H - 01-20-2016, 05:56 AM
RE: Roman Numeral Function - Gerald H - 01-20-2016, 04:09 PM
RE: Roman Numeral Function - Gerald H - 01-21-2016, 10:45 AM
RE: Roman Numeral Function - DrD - 01-21-2016, 11:49 AM
RE: Roman Numeral Function - JMB - 01-21-2016, 01:42 PM



User(s) browsing this thread: 1 Guest(s)