📖 Scenario: You are building a simple tool to convert Roman numerals into regular numbers. Roman numerals are used in clocks, books, and old movies. Your program will help translate these symbols into numbers we use every day.
🎯 Goal: Build a program that takes a Roman numeral string and converts it into its integer value using basic C programming concepts.
📋 What You'll Learn
Create a function to map Roman numeral characters to their integer values
Use a loop to process each character in the Roman numeral string
Handle the subtraction rule where a smaller numeral before a larger numeral means subtraction
Print the final integer result
💡 Why This Matters
🌍 Real World
Roman numerals are used in clocks, book chapters, movie titles, and historical documents. Converting them to integers helps in calculations and comparisons.
💼 Career
Understanding string processing, conditional logic, and loops in C is fundamental for software development, embedded systems, and working with legacy data formats.
Progress0 / 4 steps
1
Create the Roman numeral string
Create a character array called roman and initialize it with the string "MCMXCIV" which represents the Roman numeral for 1994.
DSA C
Hint
Use double quotes to create a string in C, and assign it to a character array.
2
Create a function to get integer value of Roman characters
Write a function called value that takes a char parameter r and returns the integer value of the Roman numeral character. Use switch to return 1000 for 'M', 500 for 'D', 100 for 'C', 50 for 'L', 10 for 'X', 5 for 'V', and 1 for 'I'. Return 0 for any other character.
DSA C
Hint
Use a switch statement to return the correct integer for each Roman numeral character.
3
Convert the Roman numeral to integer
Write a function called romanToInt that takes the character array roman and returns the integer value. Use a for loop to iterate through the string. For each character, compare its value with the next character's value. If the current value is less than the next, subtract it from the result; otherwise, add it. Use the value function to get integer values.
DSA C
Hint
Use a while loop to check each character and compare with the next. Add or subtract accordingly.
4
Print the converted integer
In the main function, call romanToInt with the roman string and print the returned integer using printf.
DSA C
Hint
Call romanToInt with roman and print the result using printf.
Practice
(1/5)
1. What is the main rule to convert Roman numerals to integers? Example: IV = 4, VI = 6
easy
A. Multiply values of all symbols
B. Add values of symbols, subtract if a smaller symbol is before a larger one
C. Add values only if symbols are in alphabetical order
D. Subtract values of all symbols
Solution
Step 1: Understand Roman numeral rules
Roman numerals add values of symbols, but if a smaller symbol appears before a larger one, it is subtracted.
Step 2: Apply rule to example
For IV, I (1) is before V (5), so subtract 1 from 5 = 4. For VI, V (5) before I (1), so add 5 + 1 = 6.
Final Answer:
Add values of symbols, subtract if a smaller symbol is before a larger one -> Option B
Quick Check:
IV = 4, VI = 6 [OK]
Hint: Subtract when smaller symbol precedes larger one [OK]
Common Mistakes:
Always adding without subtraction
Multiplying symbols instead of adding
Ignoring order of symbols
2. Which of the following C code snippets correctly initializes a map for Roman numeral values?
easy
A. int values = {'I':1, 'V':5, 'X':10};
B. int values[] = {I=1, V=5, X=10};
C. int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10};
D. int values[3] = {1, 5, 10};
Solution
Step 1: Understand C array initialization for character keys
In C, to map characters to values, an array indexed by character offset is used, e.g., values['I'-'A'] = 1.
Step 2: Check each option
int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10}; uses correct syntax for sparse initialization. Options B and C use invalid syntax for C. int values[3] = {1, 5, 10}; initializes array without mapping to characters.
Final Answer:
int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10}; -> Option C
Quick Check:
Correct C syntax for char-indexed array [OK]
Hint: Use array with char offset for mapping in C [OK]
Common Mistakes:
Using dictionary syntax not valid in C
Not indexing array by character offset
Initializing array without specifying indices
3. What is the output of this C code snippet?
char *s = "MCMXCIV";
int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50, ['C'-'A']=100, ['D'-'A']=500, ['M'-'A']=1000};
int result = 0, i = 0;
while (s[i]) {
int curr = values[s[i]-'A'];
int next = values[s[i+1]-'A'];
if (next > curr) result -= curr;
else result += curr;
i++;
}
printf("%d", result);
medium
A. Error
B. 1904
C. 1494
D. 1994
Solution
Step 1: Trace each character and apply addition/subtraction
String: M(1000), C(100), M(1000), X(10), C(100), I(1), V(5). Check pairs: M(1000) next C(100) -> 100 < 1000? No, add 1000 C(100) next M(1000) -> 1000 > 100, subtract 100 M(1000) next X(10) -> 10 < 1000, add 1000 X(10) next C(100) -> 100 > 10, subtract 10 C(100) next I(1) -> 1 < 100, add 100 I(1) next V(5) -> 5 > 1, subtract 1 V(5) next 0, add 5
Step 2: Calculate total
1000 - 100 + 1000 - 10 + 100 - 1 + 5 = 1994
Final Answer:
1994 -> Option D
Quick Check:
MCMXCIV = 1994 [OK]
Hint: Subtract when next symbol is larger [OK]
Common Mistakes:
Adding all values without subtraction
Off-by-one error accessing next character
Incorrect mapping of Roman symbols
4. Find the bug in this C code for Roman to Integer conversion:
int romanToInt(char *s) {
int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50, ['C'-'A']=100, ['D'-'A']=500, ['M'-'A']=1000};
int result = 0, i = 0;
while (s[i]) {
int curr = values[s[i]-'A'];
int next = values[s[i+1]-'A'];
if (curr < next) result += curr;
else result -= curr;
i++;
}
return result;
}
medium
A. The addition and subtraction logic is reversed
B. Array indexing causes out-of-bounds error
C. Values array is not initialized correctly
D. Function does not handle empty string
Solution
Step 1: Analyze addition and subtraction conditions
Code adds current value if curr < next, else subtracts. This is opposite of Roman rules where smaller before larger means subtract.
Step 2: Correct logic
It should subtract curr if curr < next, else add curr.
Final Answer:
The addition and subtraction logic is reversed -> Option A
Quick Check:
Subtract when smaller before larger [OK]
Hint: Subtract if current < next, else add [OK]
Common Mistakes:
Swapping add and subtract conditions
Ignoring last character's next value
Not initializing values array properly
5. Given the Roman numeral string "XLIX", which of the following C functions correctly returns its integer value?
hard
A. int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A'], next=values[s[i+1]-'A']; if(curr
B. int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A'], next=values[s[i+1]-'A']; if(curr>next) res-=curr; else res+=curr; i++; } return res; }
C. int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A']; res+=curr; i++; } return res; }
D. int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A'], next=values[s[i+1]-'A']; if(curr<=next) res-=curr; else res+=curr; i++; } return res; }
Solution
Step 1: Check logic for subtraction and addition
int romanToInt(char *s) { int values[26] = {['I'-'A']=1, ['V'-'A']=5, ['X'-'A']=10, ['L'-'A']=50}; int res=0,i=0; while(s[i]) { int curr=values[s[i]-'A'], next=values[s[i+1]-'A']; if(curr