Question

Write a function that converts an integer number in the range [1, 2000] into Roman Numerals....

Write a function that converts an integer number in the range [1, 2000] into Roman Numerals.

void IntToRoman(int n, char *roman);

Write a main() program to test and demonstrate your function

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Coding

#include<iostream>
using namespace std;
int digitHere(char num1, char num2, int i, char *c)
{
   c[i++] = num1;
   c[i++] = num2;
   return i;
}

int digit(char ch, int n, int i, char *c)
{
   for (int j = 0; j < n; j++)
       c[i++] = ch;
   return i;
}

void IntToRoman(int num1)
{
   char c[2000];
   int i = 0;

   if (num1 <= 0)
   {
       cout<<"Invalid number"<<endl;
       return;
   }

   while (num1 != 0)
   {
       if (num1 >= 1000)
       {
           i = digit('M', num1/1000, i, c);
           num1 = num1%1000;
       }

       else if (num1 >= 500)
       {
           if (num1 < 900)
           {
           i = digit('D', num1/500, i, c);
           num1 = num1%500;
           }
           else
           {
               // Add C and M after index i/.
               i = digitHere('C', 'M', i, c);
               num1 = num1%100 ;
           }
       }

       else if (num1 >= 100)
       {
           if (num1 < 400)
           {
               i = digit('C', num1/100, i, c);
               num1 = num1%100;
           }

           else
           {
               i = digitHere('C','D',i,c);
               num1 = num1%100;
           }
       }

       else if (num1 >= 50 )
       {
           if (num1 < 90)
           {
               i = digit('L', num1/50,i,c);
               num1 = num1%50;
           }

           else
           {
               i = digitHere('X','C',i,c);
               num1 = num1%10;
           }
       }
       else if (num1 >= 10)
       {
           if (num1 < 40)
           {
               i = digit('X', num1/10,i,c);
               num1 = num1%10;
           }

           else
           {
               i = digitHere('X','L',i,c);
               num1 = num1%10;
           }
       }

       else if (num1 >= 5)
       {
           if (num1 < 9)
           {
               i = digit('V', num1/5,i,c);
               num1 = num1%5;
           }

           else
           {
               i = digitHere('I','X',i,c);
               num1 = 0;
           }
       }

       else if (num1 >= 1)
       {
           if (num1 < 4)
           {
               i = digit('I', num1,i,c);
               num1 = 0;
           }

           else
           {
               i = digitHere('I', 'V', i, c);
               num1 = 0;
           }
       }
   }

   cout<<"Roman numeral is: "<<endl;
   for (int j = 0; j < i; j++)
       cout<< c[j];
   cout<<endl;
}

int main()
{
   int num1;
   cout<<"please enter the value of which you want to convert in roman ::";
   cin>>num1;
   IntToRoman(num1);
   return 0;
}

output:

if you still have any Problem regarding this question please comment and if you like my code please appreciate me by thumbs up thank you.........

Add a comment
Know the answer?
Add Answer to:
Write a function that converts an integer number in the range [1, 2000] into Roman Numerals....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program in C++ that converts a number entered in Roman numerals to a positive...

    Write a program in C++ that converts a number entered in Roman numerals to a positive integer. Your program should consist of a class, say, romanType. An object of type romanType should do the following: Store the number as a Roman numeral. Convert and store the number as a positive integer. Print the number as a Roman numeral or positive integer as requested by the user. The integer values of the Roman numerals are: M = 1000; D = 500;...

  • Write the roman.cpp implementation file that converts a number entered in Roman numerals to a positive...

    Write the roman.cpp implementation file that converts a number entered in Roman numerals to a positive integer. Your program should consist of a class, say, romanType. An object of type romanType should do the following: Step a: Store the number as a Roman numeral. Step b: Convert and store the number as a positive integer. Step c: Print the number as a Roman numeral or positive integer as requested by the user. Step d: Test your program using the following...

  • C++ Write a program that converts anumber entered in Roman numerals to decimal.

    In C++ Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say,romanType. An object of typeromanTypeshould do the following:a. Store the number as a Romannumeral.b. Convert and store the number into decimalform.c. Print the number as a Roman numeral ordecimal number as requested by the user.The decimal values of the Roman numerals are:M 1000D 500C 100L 50X 10V 5I 1d. Test your program using the followingRoman numerals: MCXIV, CCCLIX,...

  • (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers....

    (c++) Write a program that converts a positive integer into the Roman number system.(c++) Roman numbers. Write a program that converts a positive integer into the Roman number system. The Roman number system has digits I 1 V 5 X 10 L 50 C 100 D 500 M 1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately....

  • Write a program to reverse an integer number by using a function called reverse. The program...

    Write a program to reverse an integer number by using a function called reverse. The program reads in an integer number and prints its reverse order. Note that the function receives the number via a pointer, and uses the pointer to write the reverse number on the main function. The function MUST be used AS IS: void reverse(int *n) Language in C Bonus Problem: Number Reverser reverse c Write a program to reverse an integer number by using a function...

  • Write a C program to convert a given integer to roman number.Roman numerals are represented by...

    Write a C program to convert a given integer to roman number.Roman numerals are represented by 7different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D 500M 1000

  • Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its...

    Write and test a function toDecimal() that converts a roman number such as MCMLXXVII to its decimal number representation. Write a main program to test the function. Your function should have two arguments - the roman number as a string, and an error processing function. Write a helper function that will return the numeric value of each of the letters used in roman numbers. Then convert the string argument as follows look at the first two characters. If the first...

  • *KEEP IT SIMPLE PLEASE* Write a program that allows the user to convert any integer number...

    *KEEP IT SIMPLE PLEASE* Write a program that allows the user to convert any integer number to Roman numeral and the other way around. - You should create and print a menu that allows the user to select either Roman to Int or Int to Roman. - If one option is selected then you should promote the user to enter the number and then you print the equivalence in the other system. - Validate input, i.e Roman numerals should be...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • Write a program that contains the function measure() that is to accept a long integer number...

    Write a program that contains the function measure() that is to accept a long integer number total and the addresses of the integer variables inches, feet, yards, and miles. The passed long integer represents the total number of inches, and the function is to determine the number of miles, yards, feet, and inches in the passed value, writing these values directly into the respective variables declared in the calling function. This function will be called from the main program and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT