There being only 7 symbols to consider (IVXLCDM), conversion is easily achieved in any number of ways. In general, numerals are formed from left to right, largest value to smallest. However, if a smaller value precedes a larger value, the smaller value is subtracted from the larger value (or is negated). This can lead to problems such as IVX. Reading left to right this would become 10 - ( 5 - 1 ) = 10 - 4 = 6. There's nothing wrong with this, but most people would accept 6 = VI, not IVX.
The problem is there has never been an official standard relating to how Roman numerals are formed. Decimal 1999 could be represented as MCMXCIX or MIM or MDCCCCLXXXXVIIII or even a mixed format like MCMXCVIIII. All are intrinsically correct. However, only the first example conforms to what many would consider to be the "unofficial" standard, whereby certain combinations are no longer permitted (such as IIII, IM and VX).
This standard has been incorporated into the following code.
#include <iostream>
using namespace std;
int main()
{
char roman[11];
int decimal[10];
memset( roman, 0, 11 );
memset( decimal, 0, 10 * sizeof( int ));
cout << endl;
cout << "Enter a Roman number (max. 10 chars from I, V, X, L, C, D or M): ";
cin.getline( roman, 11, '\n' );
strupr( roman ); // convert to uppercase for consistency
// check validity, including all invalid combinations
if( !strlen( roman )
( strspn( roman, "IVXLCDM") != strlen( roman ))
( strstr( roman, "IIII" ))
( strstr( roman, "XXXX" ))
( strstr( roman, "CCCC" ))
( strstr( roman, "MMMM"))
( strstr( roman, "IL" ))
( strstr( roman, "IC" ))
( strstr( roman, "ID" ))
( strstr( roman, "IM" ))
( strstr( roman, "XD" ))
( strstr( roman, "XM" ))
( strstr( roman, "VX" ))
( strstr( roman, "VL" ))
( strstr( roman, "VC" ))
( strstr( roman, "VD" ))
( strstr( roman, "VM" ))
( strstr( roman, "LC" ))
( strstr( roman, "LD" ))
( strstr( roman, "LM" ))
( strstr( roman, "DM" ))
( strstr( roman, "IIV" ))
( strstr( roman, "IIX" ))
( strstr( roman, "XXL" ))
( strstr( roman, "XXC" ))
( strstr( roman, "CCD" ))
( strstr( roman, "CCM" )))
{
cout << roman << " is not a valid roman number." << endl;
return( -1 );
}
// convert to decimal, in reverse order.
int c = 9, total = 0;
while( c >= 0 )
{
switch( roman[c] )
{
case('I'): decimal[c] = 1; break;
case('V'): decimal[c] = 5; break;
case('X'): decimal[c] = 10; break;
case('L'): decimal[c] = 50; break;
case('C'): decimal[c] = 100; break;
case('D'): decimal[c] = 500; break;
case('M'): decimal[c] = 1000; break;
}
if( c < 9 ) // subtraction required?
if( decimal[c] < decimal[c+1] )
decimal[c] *= (-1); // negate
// update total.
total += decimal[c--];
}
cout << "Roman " << roman << " is decimal " << total << endl;
return( 0 );
}
The following code demonstrates how to convert decimal numerals to Roman numerals in the range 1 to 3999. Although there are some little-known conventions that allow numbers in excess of 3999 to be notated using special symbols, annotations and spacings to mulitply smaller numbers to produce larger numbers, the accepted convention states that no individual symbol may be repeated more than 3 times in succession (thus MMMM (4000) would not be allowed). Also, although there is nothing intrinsically wrong with using numerals such as IC to mean 99, the accepted convention with subtractive pairs states that I may only precede X and V, X may only precede C and L, and C may only precede M and D, while D, L and V may not be used as a prefix for any subtractive pair (thus ruling out VL for 45). Using the accepted convention makes it that much easier to code the conversion, however it is only a convention and there is no actual standard with regards Roman numerals. The accepted convention is simply the one that the majority of people will be most familiar with.
The only time we really break from convention is when designing clock faces, as using the non-conventional numeral IIII (4) is more aesthetically pleasing than IV. It not only gives a sense of balance to the VIII on the other side, but it also divides the clock face into three distinct regions, where only I appears in the first region (1, 2, 3 and 4), V appears in the second region (5, 6, 7 and 8) and X in the last region (9, 10, 11 and 12).
#include<iostream>
#include<string>
std::string roman(unsigned int dec)
{
std::string roman;
if( dec && dec<4000 )
{
while(dec>=1000) { roman+="M"; dec-=1000; }
if(dec>=900) { roman+="CM"; dec-=900; }
if(dec>=500) { roman+="D"; dec-=500; }
if(dec>=400) { roman+="CD"; dec-=400; }
while(dec>=100) { roman+="C"; dec-=100; }
if(dec>=90) { roman+="XC"; dec-=90; }
if(dec>=50) { roman+="L"; dec-=50; }
if(dec>=40) { roman+="XL"; dec-=40; }
while(dec>=10) { roman+="X"; dec-=10; }
if(dec>=9) { roman+="IX"; dec-=9; }
if(dec>=5) { roman+="V"; dec-=5; }
if(dec>=4) { roman+="IV"; dec-=4; }
while(dec--) {roman+="I"; }
}
return(roman);
}
int main()
{
for(unsigned int i=1; i<4000; ++i )
std::cout<<i<<" = "<<roman(i).c_str()<<std::endl;
return(0);
}
Output:
1 = I
2 = II
3 = III
4 = IV
5 = V
6 = VI
...
3997 = MMMCMXCVII
3998 = MMMCMXCVIII
3999 = MMMCMXCIX
There is a C program that exists, that will convert numbers into Roman numerals. It is available free of charge and the source code is posted online.
how to write a program that counts automorphic number from 1 to 999
#include
A total is a variable that accumulates the sum of several numbers. Answer is based on C How to Program (6th Edition)
(defun max3 (a b c) (cond ((> a b) (cond ((> a c) a) (t c))) ((> b c) b) (t c) ) )
To write a C++ program to display the student details using class and array of object.
On Converting between Arabic and Roman Numbers ,we get : C in roman numerals is : 100
C
'C' represents the number 100 in Roman numerals.
By learning how to program on C+.
fdsgfhgdfhgdf
The letter C in Roman numerals is 100.The letter C in Roman numerals is 100.The letter C in Roman numerals is 100.The letter C in Roman numerals is 100.The letter C in Roman numerals is 100.The letter C in Roman numerals is 100.The letter C in Roman numerals is 100.The letter C in Roman numerals is 100.The letter C in Roman numerals is 100.
good morning
Converting between Arabic and Roman Numbers we get that : roman numeral value for 741 is DCCXLI as C=100 .
110,100 in Roman numerals is (C) (X) (C) or CXC with a line over C and X; 1,000 is M
program to find maximum of two numbers using pointers
largest of a, b, c :a > b ? a > c ? a : c : b > c ? b : c
On converting between Arabic and Roman Numbers ,we get : c as 100 and m=1000 .