Question

In: Computer Science

Problem Statement Create a program that will convert a decimal value into roman numeral, and output...

Problem Statement Create a program that will convert a decimal value into roman numeral, and output the result. The program will ask for an integer input, assume all inputs are valid, and the program should not end unless the user tells you to.

Refactoring Once your program is running correctly, put your conversion algorithm into a function named d_to_r(), which takes a string as parameter and return an int. You are allowed to create more helper functions if needed.

Link for Roman numerals: http://sierra.nmsu.edu/morandi/coursematerials/RomanNumerals.html

Solutions

Expert Solution

//I am little bit confused for your question as Refactoring Once your program is running correctly, put your conversion algorithm into a function named d_to_r(), which takes a string as parameter and return an int. SO i have provide roman to decimal also.

If any more reQuirement kindly ask.

PROGRAM

#include<stdio.h>
#include<stdlib.h>
int digit(char c);
void dtor(int num){
int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; //base values
char *symbol[] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; //roman symbols
int i = 0;

while(num){ //repeat process until num is not 0
while(num/decimal[i]){ //first base value that divides num is largest base value
printf("%s",symbol[i]); //print roman symbol equivalent to largest base value
num -= decimal[i]; //subtract largest base value from num
}
i++; //move to next base value to divide num
}
}
//METHOD to convert the roman numbers to decimal
//take argument as string and returns integer value
long int d_to_r(char roman_Number[1000])
{
int i=0;
long int number =0;
//loop to extract one by one characters from the string
while(roman_Number[i]){
//condition for invalid digit
if(digit(roman_Number[i]) < 0){
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}
//condition for invalid roman number
if((strlen(roman_Number) -i) > 2){
if(digit(roman_Number[i]) < digit(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}
//condition to form the decimal numbers by using digit() method
if(digit(roman_Number[i]) >= digit(roman_Number[i+1]))
number = number + digit(roman_Number[i]);
else{
number = number + (digit(roman_Number[i+1]) - digit(roman_Number[i]));
i++;
}
i++;
}
//return the number
return number;

}

//method to set the decimal value for corresponding roman character
int digit(char c)
{
int value=0;
switch(c){
case 'I': value = 1; break;
case 'V': value = 5; break;
case 'X': value = 10; break;
case 'L': value = 50; break;
case 'C': value = 100; break;
case 'D': value = 500; break;
case 'M': value = 1000; break;
case '': value = 0; break;
default: value = -1;
}
//return the numeric value
return value;
}

int main()
{
   int n;
   char ch;
   char roman_Number[1000];
int i=0;
long int number =0;
//infinite loop for decimal to roman
   while(1)
   {
      printf(" Enter a decimal value");
      fflush(stdin);
      scanf("%d",&n);//ask for decimal value
      printf("%d = ",n);
      dtor(n);//call the method to display the roman equivalent
      //ask for further convertion to user
      printf(" Do you want to continue[Y/N]");
      fflush(stdin);
      scanf("%c",&ch);
      if(ch=='n' || ch=='N')
      break;
   }
   //infinite loop for roman to decimal
while(1)
   {
      //ask for roman value
   printf(" Enter any roman number (Valid digits are I, V, X, L, C, D, M): ");
   scanf("%s",roman_Number);
   //call the method to convert roman to decimal
       number=d_to_r(roman_Number);
       printf("Its decimal value is : %ld",number);
       //ask for further convertion to user
       printf(" Do you want to continue[Y/N]");
      fflush(stdin);
      scanf("%c",&ch);
      if(ch=='n' || ch=='N')
      break;
   }
}

OUTPUT


Related Solutions

Conversion of numeral to roman number: Create a FLOWCHART and C++ PROGRAM that prompts the user...
Conversion of numeral to roman number: Create a FLOWCHART and C++ PROGRAM that prompts the user to enter a number (from 1-3000 only) then output the number and its corresponding roman numeral number. ***validate the input (it must be 1-3000 only) SAMPLE OUTPUT: Enter a Number: 0 Number should be from 1-3000 only (Terminated) Enter a Number: -5 Number should be from 1-3000 only (Terminated) Enter a Number: 3500 Number should be from 1-3000 only (Terminated) Enter a Number: 1994...
Write a simple java program to list roman numeral for a given range of numbers. Roman...
Write a simple java program to list roman numeral for a given range of numbers. Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000 Roman numerals are usually written largest to smallest from left to right. But a number like 4 is not written as IIII. It is written as IV. Because...
Write a java program to convert a positive integer value to a roman number. Your program...
Write a java program to convert a positive integer value to a roman number. Your program should print a description, prompt them for how many numbers they need to convert and then loop that many times. Each time the program needs to prompt for a number and output the equivalent roman numeral. If the user enters a number less than 1 then output an error message. See sample output below: **************************************************** * Welcome to the Roman Numeral Converter!          * *...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number...
Problem: Convert the following binary number to decimal. 1. 110101.101 Problem: Convert the following decimal number to fractional binary representation. 1. 103.5625
Exercise 1: Write a program that converts a number entered in Roman numerals to decimal. Your...
Exercise 1: Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say, Roman. An object of type Roman should do the following: Store the number as a Roman numeral. Convert and store the number into decimal. Print the number as a Roman numeral or decimal number as requested by the user. The decimal values of the Roman numerals are: M 1000 D 500 C 100 L 50 X 10...
Please convert decimal value -32760 into 2's complement binary value
Please convert decimal value -32760 into 2's complement binary value
Using Python, create a program that will act as a number convertor. This program will convert...
Using Python, create a program that will act as a number convertor. This program will convert an input decimal integer into binary and hexadecimal formats. a) Define a main function named numberconvertor(). Inside the main function, write all the logic of your code. [5% marks] b) Looping indefinitely (Hint: use infinite while loop), the program should give the user the 3 options given below and allow the user to select one among them. [15% marks] 1. converting a decimal number...
ASAP (Convert decimals to fractions) Write a program that prompts the user to enter a decimal...
ASAP (Convert decimals to fractions) Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: read the decimal number as a string, extract the integer part and fractional part from the string, and use the Rational class in Listing 13.13 to obtain a rational number for the decimal number. Use the template at https://liveexample.pearsoncmg.com/test/Exercise13_19Test.txt for your code. Sample Run 1 Enter a decimal number: 3.25 The fraction number is 13/4...
How did the Roman devotion to mos maiorum create contribute to the succession problem of the...
How did the Roman devotion to mos maiorum create contribute to the succession problem of the Roman Empire? In your essay make sure you define mos maiorum, the nature of the succession problem, and why the 'solutions' failed to work. Give SPECIFIC examples! small topic History
Write a program to convert the input numbers to another number system. 1. Decimal to Binary...
Write a program to convert the input numbers to another number system. 1. Decimal to Binary 2. Binary to Decimal 3. Hexadecimal to Decimal 4. Decimal to Hexadecimal 5. Binary to Hexadecimal 6. Hexadecimal to Binary The user will type in the input number as following: Binary number : up to 8 bits Hexadecimal number: up to 2 bytes Decimal number: Less than 256 As a result, print out the output after the conversion with their input numbers. The program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT