Roman Numerals Filename: ROMAN Roman numerals are an ancient numbering system. I
ID: 3708373 • Letter: R
Question
Roman Numerals Filename: ROMAN Roman numerals are an ancient numbering system. If you are unfamiliar with roman numerals, refer to the sheet at the back of the problem set. The Problem: Add two roman numerals, expressing the result in roman numerals. The Input: Several pairs of roman numerals. Only the capital letters I, V, X, L, C, D and M will be used. Each roman numeral will be on a separate line, starting in column one. The roman numerals and their sums are guaranteed to be within the range of 1 to 3999, inclusive. The Output: For each pair of roman numerals, print the sum, expressed in roman numerals. (Capital letters only, please.) Sample Input: VI XXVII DXIV Sample Output: IX MMCDXCVIIIExplanation / Answer
#include<stdio.h>
// Function to accept a roman character and return its numeric value
int integerValue(char c)
{
// Checks the character and returns its corresponding number
// Considered for both lower case and upper case character
switch(c)
{
case 'I':
case 'i':
return 1;
case 'V':
case 'v':
return 5;
case 'X':
case 'x':
return 10;
case 'L':
case 'l':
return 50;
case 'C':
case 'c':
return 100;
case 'D':
case 'd':
return 500;
case 'M':
case 'm':
return 1000;
// For invalid roman character
default:
return 0;
}// End of switch case
}// End of function
// Function to accept a roman number and converts it to integer number and returns it
int convertToInteger(char roman[], int len)
{
// Loop variable
int c;
// To store the converted integer number
int integerNo = 0;
// Loops from length minus one index position to zero index position of roman array
for(c = len-1; c > -1; c--)
{
// Checks the character at c index position and adds its respective integer value to integerNo
switch(roman[c])
{
case 'I':
case 'i':
// Calls the function to get the integer value of the roman character
// Adds the integer value to integerNo
integerNo += integerValue(roman[c]);
break;
case 'V':
case 'v':
case 'X':
case 'x':
case 'L':
case 'l':
case 'C':
case 'c':
case 'D':
case 'd':
case 'M':
case 'm':
// Checks if the loop variable value is not zero
// and integer value of c - 1 index position of roman character is less than the integer value of roman character at c index position
if(c != 0 && (integerValue(roman[c - 1]) < integerValue(roman[c])))
{
// Subtract the value from integer value of roman character at c - 1 index position from
// integer value of roman character at c index position
// Adds the result to integerNo
integerNo += integerValue(roman[c]) - integerValue(roman[c - 1]);
// Decrease the counter value by one
c--;
}// End of if condition
// Otherwise
else
// Add the integer value of roman character at c index position with integerNo
integerNo += integerValue(roman[c]);
break;
}// End of switch - case
}// End of for loop
// Returns the integer number
return integerNo;
}// End of for loop
// CONVERSION FROM INTEGER NUMBER TO ROMAN
// To add corresponding base symbols in the array to handle cases which follow subtractive notation.
// Base symbols are added index 'ba'.
int subDigit(char firstNo, char secondNo, int ba, char *charNo)
{
charNo[ba++] = firstNo;
charNo[ba++] = secondNo;
return ba;
}// End of function
// To add symbol 'ch' len times after index indexPos in c[]
int addDigit(char ch, int len, int indexPos, char *charNo)
{
// Loop variable
int d;
// Loops till len
for (d = 0; d < len; d++)
charNo[indexPos++] = ch;
return indexPos;
}// End of function
// Function to convert decimal to Roman Numerals
void printRoman(int integerNo)
{
// To store roman character
char charRoman[10001];
// Loop variable
int indexPos = 0, c;
// Checks if integerNo is negative or zero
if (integerNo <= 0)
{
printf("Invalid number %d", integerNo);
return;
}// End of if condition
// Loop to convert decimal number to roman numerals
// Loops till number is not equals to zero
while (integerNo != 0)
{
// Checks if base value of integerNo is greater than 1000
if (integerNo >= 1000)
{
// Add 'M' integerNo / 1000 times after index indexPos
indexPos = addDigit('M', integerNo / 1000, indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 1000;
}// End of if condition
// Checks if base value of number is greater than or equal to 500
else if (integerNo >= 500)
{
// Checks to add base symbol to the character array
if (integerNo < 900)
{
// Add 'D' integerNo / 1000 times after index indexPos
indexPos = addDigit('D', integerNo / 500, indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 500;
}// End of if condition
// Otherwise to handle subtractive notation in case of number
// having digit as 9 and adding corresponding base symbol
else
{
// Add C and M after index indexPos
indexPos = subDigit('C', 'M', indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 100 ;
}// End of else
}// End of else if condition
// Checks if base value of number is greater than or equal to 100
else if (integerNo >= 100)
{
// Checks to add base symbol to the character array
if (integerNo < 400)
{
indexPos = addDigit('C', integerNo / 100, indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 100;
}// End of if condition
// Otherwise3, to handle subtractive notation in case of number
// having digit as 4 and adding corresponding base symbol
else
{
indexPos = subDigit('C','D', indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 100;
}// End of else
}// End of else if condition
// Checks if base value of number is greater than or equal to 50
else if (integerNo >= 50 )
{
// Checks to add base symbol to the character array
if (integerNo < 90)
{
indexPos = addDigit('L', integerNo / 50, indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 50;
}// End of if condition
// Otherwise, to handle subtractive notation in case of number
// having digit as 9 and adding corresponding base symbol
else
{
indexPos = subDigit('X','C', indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 10;
}// End of else
}// End of else if condition
// Checks if base value of number is greater than or equal to 10
else if (integerNo >= 10)
{
// Checks to add base symbol to the character array
if (integerNo < 40)
{
indexPos = addDigit('X', integerNo / 10, indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 10;
}// End of if condition
// Otherwise, to handle subtractive notation in case of
// number having digit as 4 and adding corresponding base symbol
else
{
indexPos = subDigit('X','L', indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 10;
}// End of else
}// End of else if condition
// Checks if base value of number is greater than or equal to 5
else if (integerNo >= 5)
{
// Checks to add base symbol to the character array
if (integerNo < 9)
{
indexPos = addDigit('V', integerNo / 5, indexPos, charRoman);
// Calculates the remainder
integerNo = integerNo % 5;
}// End of if condition
// Otherwise, to handle subtractive notation in case of number
// having digit as 9 and adding corresponding base symbol
else
{
indexPos = subDigit('I','X', indexPos, charRoman);
integerNo = 0;
}// End of else
}// End of else if condition
// Otherwise checks if base value of number is greater than or equal to 1
else if (integerNo >= 1)
{
if (integerNo < 4)
{
indexPos = addDigit('I', integerNo, indexPos, charRoman);
integerNo = 0;
}// End of if condition
// Otherwise, to handle subtractive notation in case of
// number having digit as 4 and adding corresponding base symbol
else
{
indexPos = subDigit('I', 'V', indexPos, charRoman);
integerNo = 0;
}// End of else
}// End of else if condition
}// End of while loop
// Displays the roman number
printf("Roman numeral is: ");
// Loops till indexPos
for (c = 0; c < indexPos; c++)
printf("%c", charRoman[c]);
}// End of function
// main function definition
int main()
{
// Loop variable
int i;
// To store first and second number length
int firstLen, secondLen;
// To store converted first and second number
int firstNo, secondNo;
// To store the calculated total
int total = 0;
// To store first and second roman number
char firstRoman[100], secondRoman[100];
// Accepts first roman number
printf(" Enter the first Roman Number: ");
scanf("%s", firstRoman);
// Accepts second roman number
printf(" Enter the first Roman Number: ");
scanf("%s", secondRoman);
// Calculates first roman number length
// Loops till '' character and calculates the length
for(firstLen = 0; firstRoman[firstLen] != ''; firstLen++)
;// Do nothing statement
// Calculates second roman number length
// Loops till '' character and calculates the length
for(secondLen = 0; secondRoman[secondLen] != ''; secondLen++)
;// Do nothing statement
// Calls the function to convert the first roman number to integer and stores it in firstNo
firstNo = convertToInteger(firstRoman, firstLen);
// Calls the function to convert the second roman number to integer and stores it in secondNo
secondNo = convertToInteger(secondRoman, secondLen);
// Calculates the total
total = firstNo + secondNo;
// Calls the function to convert the total to roman number
printRoman(total);
}// End of main function
Sample Output 1:
Enter the first Roman Number: III
Enter the first Roman Number: VI
Roman numeral is: IX
Sample Output 2:
Enter the first Roman Number: XXIII
Enter the first Roman Number: XXVII
Roman numeral is: L
Sample Output 3:
Enter the first Roman Number: MCMLXXXIV
Enter the first Roman Number: DXIV
Roman numeral is: MMCDXCVIII
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.