roman numeral converter

This is a discussion on roman numeral converter within the C++ Programming forums, part of the General Programming Boards category; I want to ask if this is correct and will not make any mistake in coverting arabic numbers to roman ...

  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 10: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
    32,852
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 07:23 PM
  5. implicit declatation of function 'int toupper(...)'
    By Intimd8r in forum C Programming
    Replies: 3
    Last Post: 10-01-2001, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21