Roman Numbers :
Its a numeric system used to express numbers by using combinations of different Latin alphabets in ancient Rome. Some time this numeric system is still used.
No zero in Roman Number :
Roman numeric system don't have Zero (o) . So its always start with 1. Also it does not have negative values like modern decimal system.
Largest number which can express in Roman:
The maximum integer value which can be written in form of Roman numeric system is 3888. 3888 can be expressed in Roman as MMMDCCCLXXXVIII. This is because M,C and X can be used maximum of three times but this can't be done with D, L and V.However roman uses bar over letters to express times thousand.
__
VI = 6000
So
__________________
MMMDCCCLXXXVIII = 3888000 could be largest but we will consider 3888 as largest Roman number in our code.
Java Code to Convert Integer to Roman Numbers
package myapps; /** * * @author JavaTalk.org */ public class RomanConverter { public static void main(String arg[]) { RomanConverter rc=new RomanConverter(); for (int i=1 ; i<=100;i++) { System.out.println(i+"\t--->\t"+rc.Decimal2Roman(i)); } } public String Decimal2Roman(int num) { String[] romValues = {"M","XM","CM","D","XD","CD","C","XC","L","XL","X","IX","V","IV","I"}; int[] integerValues = {1000, 990, 900, 500, 490, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; StringBuilder result = new StringBuilder(); int i = 0; if(num>3888) { System.out.println("Please Enter lesser number"); return null; } if(num<=0) { System.out.println("Please Enter number greater than Zero"); return null; } while (num > 0) { while ((num - integerValues[i]) >= 0) { num = num - integerValues[i]; result.append(romValues[i]); } i++; } return result.toString(); } }
Output of Program is :
Roman Numbers from 1 to 100
1 ---> I 2 ---> II 3 ---> III 4 ---> IV 5 ---> V 6 ---> VI 7 ---> VII 8 ---> VIII 9 ---> IX 10 ---> X 11 ---> XI 12 ---> XII 13 ---> XIII 14 ---> XIV 15 ---> XV 16 ---> XVI 17 ---> XVII 18 ---> XVIII 19 ---> XIX 20 ---> XX 21 ---> XXI 22 ---> XXII 23 ---> XXIII 24 ---> XXIV 25 ---> XXV 26 ---> XXVI 27 ---> XXVII 28 ---> XXVIII 29 ---> XXIX 30 ---> XXX 31 ---> XXXI 32 ---> XXXII 33 ---> XXXIII 34 ---> XXXIV 35 ---> XXXV 36 ---> XXXVI 37 ---> XXXVII 38 ---> XXXVIII 39 ---> XXXIX 40 ---> XL 41 ---> XLI 42 ---> XLII 43 ---> XLIII 44 ---> XLIV 45 ---> XLV 46 ---> XLVI 47 ---> XLVII 48 ---> XLVIII 49 ---> XLIX 50 ---> L 51 ---> LI 52 ---> LII 53 ---> LIII 54 ---> LIV 55 ---> LV 56 ---> LVI 57 ---> LVII 58 ---> LVIII 59 ---> LIX 60 ---> LX 61 ---> LXI 62 ---> LXII 63 ---> LXIII 64 ---> LXIV 65 ---> LXV 66 ---> LXVI 67 ---> LXVII 68 ---> LXVIII 69 ---> LXIX 70 ---> LXX 71 ---> LXXI 72 ---> LXXII 73 ---> LXXIII 74 ---> LXXIV 75 ---> LXXV 76 ---> LXXVI 77 ---> LXXVII 78 ---> LXXVIII 79 ---> LXXIX 80 ---> LXXX 81 ---> LXXXI 82 ---> LXXXII 83 ---> LXXXIII 84 ---> LXXXIV 85 ---> LXXXV 86 ---> LXXXVI 87 ---> LXXXVII 88 ---> LXXXVIII 89 ---> LXXXIX 90 ---> XC 91 ---> XCI 92 ---> XCII 93 ---> XCIII 94 ---> XCIV 95 ---> XCV 96 ---> XCVI 97 ---> XCVII 98 ---> XCVIII 99 ---> XCIX 100 ---> C
Please comment if you have any better method or code.
0 comments:
Post a Comment