In this post we will write a java program to convert a decimal number to equivalent Roman numeral. Before we step in to the program, we will see what are the Roman symbols and its decimal values.
Roman is : IX
Enter a decimal : 30
Roman is : XXX
Enter a decimal : 2001
Roman is : MMI
Roman Symbols and its values:
Roman Symbol | Decimal value |
I | 1 |
IV | 4 |
V | 5 |
IX | 9 |
X | 10 |
XL | 40 |
L | 50 |
XC | 90 |
C | 100 |
CD | 400 |
D | 500 |
CM | 900 |
M | 1000 |
Decimal Forming Rules:
1. When a symbol appears after a larger or equal symbol, then it is added- VI = V + I = 5 + 1 = 6
- XXI = X + X + I = 10 + 10 + 1 = 21
- IV = V - I = 5 - 1 = 4
- IX = X - I = 10 - 1 = 9
Program :
With the below program, we can covert up to the maximum value 3000. If you want to covert more than 3000, just add more roman symbols in the thousands array.
Output:
Enter a decimal : 9Roman is : IX
Enter a decimal : 30
Roman is : XXX
Enter a decimal : 2001
Roman is : MMI
No comments:
Post a Comment