Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a program to convert a Roman numeral to a decimal number. The program sho

ID: 3808158 • Letter: D

Question

Design a program to convert a Roman numeral to a decimal
number. The program should read a Roman numeral. You
may read it as a string or one character at a time. Do
the conversion and then output the decimal number.

Here are the “letters” you need to know:

Symbol =   Value
I   = 1
V =   5
X =   10
L =   50
C =   100
D =   500
M   = 1,000

The Pseudocode i got was

Start

Prompt the user to enter a roman numeral

Save the input to romanNum

If romanNum is inputted as “I”

Then decimalNum is equal to 1

endif

If romanNum is inputted as “V”

Then decimalNum is equal 5

endif

If romanNum is inputted as “X”

Then decimalNum is equal to 10

endif

If romanNum is inputted as “L”

Then decimalNum is equal to 50

endif

If romanNum is inputted as “C”

Then decimalNum is equal to 100

endif

If romanNum is inputted as “D”

Then decimalNum is equal to 500

endif

If romanNum is inputted as “M”

Then decimalNum is equal to 1000

endif

Output decimalNum

Stop

Using that Pseudocode i typed

#include <iostream>
using namespace std;

int main ()
{
   int romanNum;
   int decimalNum;
  
   cout << "Enter a roman numeral: ";
   cin >> romanNum;
  
   if(romanNum == 'I') {
   decimalNum = 1;
}

if(romanNum == 'V') {
   decimalNum = 5;
}

if(romanNum == 'X') {
   decimalNum = 10;
}

if(romanNum == 'L') {
   decimalNum = 50;
}

if(romanNum == 'C') {
   decimalNum = 100;
}

if(romanNum == 'D') {
   decimalNum = 500;
}

if(romanNum == 'M') {
   decimalNum = 1000;
}
cout << decimalNum << endl;

return 0;
}

Whenever i type a Roman Numeral i get 0

I was wondering if my code is is bad or the Pseudocode i was given has an error somewhere.

DARomanNumeral Design4.exe Enter a roman numeral I Process exited after 2.061 seconds with return value Press any key to continue

Explanation / Answer

romncnvrt.cpp

#include <stdio.h>

void firstdgt(char n1, char n2);

void scnddgt(char f, int n);

char rom[1000];

int i = 0;

int main()

{
    int j;

    long num;

    printf("Enter a number to convert to roman letter: ");

    scanf("%d", &num);

    if (num <= 0)

    {
        printf("numbr not valid");
        return 0;

    }

    while (num != 0)

    {

        if (num >= 1000)

        {
           scnddgt('M', num / 1000);
            num = num - (num / 1000) * 1000;

        }

        else if (num >= 500)

        {
            if (num < (500 + 4 * 100))

            {

                scnddgt('D', num / 500);
                num = num - (num / 500) * 500;

            }

            else

            {

                firstdgt('C','M');

                num = num - (1000-100);

            }

        }

        else if (num >= 100)

        {
            if (num < (100 + 3 * 100))

            {
                scnddgt('C', num / 100);

                num = num - (num / 100) * 100;

            }

            else

            {

                firstdgt('L', 'D');

                num = num - (500 - 100);

            }

        }

        else if (num >= 50 )

        {
            if (num < (50 + 4 * 10))

            {

                scnddgt('L', num / 50);

                num = num - (num / 50) * 50;

            }

            else

            {

                firstdgt('X','C');

                num = num - (100-10);

            }

        }

        else if (num >= 10)
        {

            if (num < (10 + 3 * 10))

            {

                scnddgt('X', num / 10);

                num = num - (num / 10) * 10;

            }

            else

            {

                firstdgt('X','L');

                num = num - (50 - 10);

            }

        }

        else if (num >= 5)

        {

            if (num < (5 + 4 * 1))

            {

                scnddgt('V', num / 5);

                num = num - (num / 5) * 5;

            }

            else

            {

                firstdgt('I', 'X');

                num = num - (10 - 1);

            }

        }

        else if (num >= 1)

        {

            if (num < 4)

            {

               scnddgt('I', num / 1);

                num = num - (num / 1) * 1;

            }

            else

            {

                firstdgt('I', 'V');

                num = num - (5 - 1);

            }

        }

    }

    printf("Roman number is: ");

    for(j = 0; j < i; j++)

        printf("%c", rom[j]);

    return 0;

}

void firstdgt(char num1, char num2)

{

    rom[i++] = num1;

    rom[i++] = num2;

}

void scnddgt(char c, int n)

{

    int j;

    for (j = 0; j < n; j++)

        rom[i++] = c;

}

output:

Enter a number to convert to roman letter:500

Roman number is:D

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote