?
Solved

conversion

Posted on 1998-09-17
9
Medium Priority
?
215 Views
Last Modified: 2010-04-16
i'm still new at pascal.
Q: how do i write a program that ask the user to enter a number and then convert that number  into a roman number using a function?
pls help,need to solve this problem in 2 days
0
Comment
Question by:vinnad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Author Comment

by:vinnad
ID: 1215599
anyone who answer my Q is a real lifesaver!!!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1215600
0
 

Accepted Solution

by:
Styler earned 200 total points
ID: 1215601
program convert;

uses crt;

var number: word;

{ Number is a variable which will be an asked number. }
{ Note: }
{ Type of variable is "word" this mean that it can be 0...65535. }

    tempstr, roman: string;

{ Roman is a string variable which will be an roman number }
{ and tempstr is temporary variable. }

begin
  write(' Enter a number please: ');
  read(number);            { Asking a number to enter}
  while number>=10 do      { Converting this number to roman. }
  begin
    roman:=roman+'X';
    dec(number,10);
  end;
  while number>=5 do
  begin
    roman:=roman+'V';
    dec(number,5);
  end;
  if number=4 then
  begin
    roman:=roman+'IV';
    dec(number,4);
  end;
  while number>=1 do
  begin
    roman:=roman+'I';
    dec(number);
  end;
  write(roman);
  readkey;
end.

{    There is only one feature in this program:                     }
{ it converts number not greater then 49. This is beacause of I     }
{ have no time today (Sep 18) and I dont remember all roman digits. }
{ If you want me to give you program which will converts any number }
{ to a number with Roman digits then tell all roman digits.         }

0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
LVL 5

Expert Comment

by:scrapdog
ID: 1215602
The following program calls a function as specified.  This one only goes up to 3999 (I too don't know all of the digits).  If you want to go up to 10000 fill in the rest of the Place4 constant, and if you want to go even higher add another array.

Scrapdog



program RomanNumeralDemo;


type
  TPlaceDigits = array[0..9] of string;
const
  Place4 :TPlaceDigits = ('', 'M', 'MM' , 'MMM' , '', '', '', '', '', '');
  Place3 :TPlaceDigits = ('', 'C', 'CC' , 'CCC' , 'CD' , 'D' , 'DC' , 'DCC' , 'DCCC' , 'CM');
  Place2 :TPlaceDigits = ('', 'X', 'XX' , 'XXX' , 'XL' , 'L' , 'LX' , 'LXX' , 'LXXX' , 'XC');
  Place1 :TPlaceDigits = ('', 'I', 'II' , 'III' , 'IV' , 'V' , 'VI' , 'VII' , 'VIII' , 'IX');


{function is limited to numbers 0-3999, but can be expanded by added more places}

function ArabicToRoman(NumIn :word) :string;
var Thousands, Hundreds, Tens, Ones :word;
begin
  if NumIn > 3999 then NumIn := 3999;
  Thousands := NumIn div 1000;
  Hundreds  := NumIn mod 1000 div 100;
  Tens      := NumIn mod 1000 mod 100 div 10;
  Ones      := NumIn mod 1000 mod 100 mod 10;
  ArabicToRoman := Place4[Thousands]+Place3[Hundreds]+Place2[Tens]+Place1[Ones];
end;

var
  NumIn :word;

begin
  write('Enter a number: ');
  readln(NumIn);
  writeln('The roman numeral is: ',ArabicToRoman(NumIn));
end.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1215603
Here is another better way that is implemented by Styler's way ;-)
---------
function NumToRoman( Num : Integer ) : String;
var
      R : String;
begin
      R := '';
   while Num > 1000 do begin
         R := R + 'M';
      Dec(Num, 1000);
   end;
   while Num > 500 do begin
            R := R + 'D';
      Dec (Num, 500);
   end;
   while Num > 100 do begin
         R := R + 'C';
      Dec(Num, 100);
   end;
   while Num > 50 do begin
         R := R + 'L';
      Dec (Num, 50);
   end;
   while Num > 10 do begin
            R := R + 'X';
      Dec (Num, 10);
   end;
   while Num >= 5 do begin
         R := R + 'V';
      Dec(Num, 5);
   end;
   if Num = 4 then begin
         R := R + 'IV';
      Dec(Num, 4);
   end;
   while Num >= 1 do begin
         R := R + 'I';
      Dec(Num, 1);
   end;
   NumToRoman := R;
end;
------------
Regards,
Viktor Ivanov
0
 

Expert Comment

by:Styler
ID: 1215604

  Victornet is a very smart but this is a plagiarism!.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1215605
viktornet:

Your code (Styler's, actually :) will not correctly code the number 4 or 9 in any of the digit places.

example:  40 = XL, not XXXX
          90 = XC, not LXXXX

use the scrapdog way :)


0
 
LVL 10

Expert Comment

by:viktornet
ID: 1215606
Well 90 is LXXXX
and 40 is XXXX

And I didn't meant to copy the answer just wanted to fill the blank numbers as you won't get vey large with your code... with your code if you type 10000 it will display 1000X10s and that's not what he wants....I just added some addititional roman numbers as L M D
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var
  pt : TPoint;
begin
  pt := Point($746B6956, $726F);
  Caption := PChar(@pt);
end;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1215607
You yeah, I think Scrap dog is right about the numbers as 40 = XL

Thanks for the correction...
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Summer 2017 Scholarship Winners have been announced!
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month12 days, 20 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question