Thread: roman numeral converter

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    1

    roman numeral converter

    I want to ask if this is correct and will not make any mistake in coverting arabic numbers to roman numerals
    Also I want to ask if it is better to use modulus operator.
    Code:
    {
    	int year;
    
    	cout<<"Enter a year: ";
    	cin>>year;
    	cout<<"The Roman Numeral equivalent of "<<year<<" is ";
    	while(year>=1000)
    	{
    		cout<<"M";
    		year=year-1000;
    	}
    	if(year>=900)
    	{
    		cout<<"CM";
    		year=year-900;
    	}
    	if(year>=500)
    	{
    		cout<<"D";
    		year=year-500;
    	}
    	if(year>=400)
    	{
    		cout<<"CD";
    		year=year-400;
    	}
    	while(year>=100)
    	{
    		cout<<"C";
    		year=year-100;
    	}
    	if(year>=90)
    	{
    		cout<<"XC";
    		year=year-90;
    	}
    	if(year>=50)
    	{
    		cout<<"L";
    		year=year-50;
    	}
    	if(year>=40)
    	{
    		cout<<"XL";
    		year=year-40;
    	}
    	while(year>=10)
    	{
    		cout<<"X";
    		year=year-10;
    	}
    	if(year>=9)
    	{
    		cout<<"IX";
    		year=year-9;
    	}
    	if(year>=5)
    	{
    		cout<<"V";
    		year=year-5;
    	}
    	while(year>=1)
    	{
    		cout<<"I";
    		year=year-1;
    	}
    
    }
    Last edited by SoriNaEh; 10-06-2010 at 09:48 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well how many numbers have you tested?

    On the face of it, it seems like a workable answer. But we´re not here to do exhaustive testing for you.

    > Also I want to ask if it is better to use modulus operator.
    Well it would be a different approach.
    But whether that would be better is opinion.

    You should try it, just for the experience
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Roman numeral converter problem
    By 7heTexanRebel in forum C Programming
    Replies: 4
    Last Post: 12-14-2009, 08:52 PM
  2. Roman Numeral troubles....
    By h4rrison.james in forum C Programming
    Replies: 16
    Last Post: 01-15-2009, 09:26 PM
  3. roman numeral conversion
    By clarky101 in forum C Programming
    Replies: 8
    Last Post: 12-09-2007, 07:13 PM
  4. Roman Numeral Calculator
    By The Brain in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2005, 06:23 PM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 02:43 PM