-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathIntegerToRoman.go
46 lines (37 loc) · 1.13 KB
/
IntegerToRoman.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Converting Integer number to its Roman form
// Whenever user type any integer value, they will get the roman value for a particular integer value.
package main
import "fmt"
func main() {
var number int
fmt.Print("Enter a number : ")
fmt.Scan(&number)
fmt.Println("Roman number of", number, "is", calIntToRoman(number))
}
// applying simple mathematics
// checking of respective roman number for each digit
func calIntToRoman(number int) string {
result := ""
j := 1
// storing integer value
// help to convert it's respective roman number
intValue := [13]int{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}
romanValue := [13]string{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}
// iterating integer and roman value
// the number contains perticular integer value, based on that adds it's roman value
for i := 0; i < 13; i++ {
if number >= intValue[i] {
f := number / intValue[i]
number = number % intValue[i]
for j <= f {
result = result + romanValue[i]
j++
}
}
j = 1
}
return result
}
// Sample Input/Output
// Input : Enter a number : 15
// Output : Roman number of 15 is XV