Write a program roman which converts numbers from decimal notation into Roman numerals. roman takes a single argument, a string containing a non-negative integer, and prints its Roman numeral equivalent to standard output. g

Respuesta :

Answer:

// Program is written in C++ programming language

/ Comments are used for explanatory purpose

// Program starts here

#include<bits/stdc++.h>

using namespace std;

//Declare a function named value to represent the symbols of Roman Numerals

int value(char xy)

{

if (xy == 'I') { return 1; }

if (xy == 'V') { return 5; }

if (xy == 'X') { return 10; }

if (xy == 'L') { return 50; }

if (xy == 'C') { return 100; }

if (xy == 'D') { return 500; }

if (xy == 'M') { return 1000; }

return -1;

}

// Convert to numbers , taking Variable named input as input

int Converted(string &input)

{

// Declare a variable to store the calculated output

int calc;

// Set to 0

calc = 0;

// Iterate through input from 0 to the last character

for (int i=0; i<input.length(); i++)

{

int check1 = value(input[i]);

if (i+1 < input.length())

{

int check12= value(input[i+1]);

// Comparing both values

if (check1>= check2)

{

calc+ =check1;

}

else

{

calc+ =check2 - check1;

i++;

}

}

else

{

calc+ =check1;

}

}

return calc;

}

// Main method starts here

int main()

{

// String input declaration

string inp;

cout<"Enter any valid Roman numeral";

cout << "Result is "

<< Converted(inp);

return 0;

}