-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path19.c
More file actions
62 lines (58 loc) · 1.58 KB
/
Copy path19.c
File metadata and controls
62 lines (58 loc) · 1.58 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// This may work only till 100
#include<stdio.h>
#include<string.h>
int main()
{
char roman_digits[100];
printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M): \n");
scanf("%s", roman_digits);
int length = strlen(roman_digits);
int decimal = 0;
for (int i=0; i< length; i++ )
{
if (roman_digits[i] == 'I')
{
if ( roman_digits[i+1] == 'V' || roman_digits[i+1] == 'X' )
decimal -= 1;
else
decimal += 1;
}
else if (roman_digits[i] == 'V')
{
if ( roman_digits[i+1] == 'X' || roman_digits[i+1] == 'L' )
decimal -= 5;
else
decimal += 5;
}
else if (roman_digits[i] == 'X')
{
if ( roman_digits[i+1] == 'L' || roman_digits[i+1] == 'C' )
decimal -= 10;
else
decimal += 10;
}
else if (roman_digits[i] == 'L')
{
if ( roman_digits[i+1] == 'C' || roman_digits[i+1] == 'D' )
decimal -= 50;
else
decimal += 50;
}
else if (roman_digits[i] == 'C')
{
if ( roman_digits[i+1] == 'D' || roman_digits[i+1] == 'M' )
decimal -= 100;
else
decimal += 100;
}
else if (roman_digits[i] == 'D')
{
decimal += 500;
}
else if (roman_digits[i] == 'M')
{
decimal += 1000;
}
}
printf("Decimal number is: %d", decimal);
}