convert decimal to roman

Given an integer i need to display the corresponding roman number

Page 1 of 1

4 Replies - 15110 Views - Last Post: 28 December 2006 - 04:08 AM Rate Topic: *---- 1 Votes

#1 santoshbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-December 06

convert decimal to roman

Posted 27 December 2006 - 03:16 AM

M=1000 | D=500 | C=100 | L=50 | X=10 | V=5 | I=1
MDCLXVI = 1000 + 500 + 100 + 50 + 10 + 5 + 1 = 1666.

but i need to convert the given integer in the form of 1000,500,100,50,10,5 and 1s

can anyone please help on some logics as how to do it
Is This A Good Question/Topic? 0
  • +

Replies To: convert decimal to roman

#2 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1256
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: convert decimal to roman

Posted 27 December 2006 - 04:10 AM

can you show us what code you've attempted thus far?
Was This Post Helpful? 0
  • +
  • -

#3 may4life  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 05-November 06

Re: convert decimal to roman

Posted 27 December 2006 - 10:17 AM

first of all you need to figure out which is the greatest value a roman number can have. As you mention it is 1000 (M).
If your number is >= 1000, you will then need to display how many thousands it has, each time subtracting 1000 from your original number.
i.e
if (number >= 1000)
		{
			cout << "M";
			number-=1000;		
		}


Then, with the same logic, use the display-subtract method for the number being >= to 500, then >= 100, then >=50 and so on until your number reaches 0.
Try it out and post some code and we'll all definitely help you out here.

This post has been edited by may4life: 27 December 2006 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 may4life  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 05-November 06

Re: convert decimal to roman

Posted 28 December 2006 - 03:16 AM

The next step would be to contain this code in a loop until the number is no longer in that field.
i.e
while (number >= 1000)
	{
		if (number >= 1000)
		{
			cout << "M";
			number-=1000;		
		}
	}


After this you implement for 500, 100, 50 and so on until you get to the code
while (number >= 1)
	{
		if (number >= 1)
		{
			cout << "I";
			number-=1;		
		}
	}


And that's it!! Decimal to Roman in no time. This is one way of doing it, im sure there are loads of other ways if you sit down n think a bit :)

This post has been edited by may4life: 28 December 2006 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#5 m2s87  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 390
  • Joined: 28-November 06

Re: convert decimal to roman

Posted 28 December 2006 - 04:08 AM

Quote

And that's it!! Decimal to Roman in no time. This is one way of doing it, im sure there are loads of other ways if you sit down n think a bit :)


Well 1 way to do this, is use my c++ snippet that i added yesterday. :ph34r:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1