answersLogoWhite

0


Best Answer

#include<iostream>

#include<sstream>

#include<exception>

std::string decimal_to_roman (unsigned num) {

std::stringstream ss {};

while (num>0) {

if (num>10000)

throw std::range_error (

"ERROR: decimal_to_roman (unsigned num) [num is out of range]");

else if (num==10000) { ss<<"[M]"; num-=10000; }

else if (num>=9000) { ss<<"[CM]"; num-=9000; }

else if (num>=5000) { ss<<"[D]"; num-=5000; }

else if (num>=4000) { ss<<"[CD]"; num-=4000; }

else if (num>=1000) { ss<<"M"; num-=1000; }

else if (num>=900) { ss<<"CM"; num-=900; }

else if (num>=500) { ss<<"D"; num-=500; }

else if (num>=400) { ss<<"CD"; num-=400; }

else if (num>=100) { ss<<"C"; num-=100; }

else if (num>=90) { ss<<"XC"; num-=90; }

else if (num>=50) { ss<<"L"; num-=50; }

else if (num>=40) { ss<<"XL"; num-=40; }

else if (num>=10) { ss<<"X"; num-=10; }

else if (num==9) { ss<<"IX"; num-=9; }

else if (num>=5) { ss<<"V"; num-=5; }

else if (num==4) { ss<<"IV"; num-=4; }

else if (num>=1) { ss<<"I"; num-=1; }

}

return ss.str();

}

int main (void) {

for (unsigned n=1; n<=10000; ++n) {

try {

std::cout << n << "\t = " << decimal_to_roman(n) << std::endl;

}

catch (std::range_error& e) {

std::cerr<<e.what()<<std::endl;

break;

}

}

}

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a program in C plus plus print 1 to 10000 in Roman numerals?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a c program to print roman letters?

good morning


What are all the numbers in Roman number form?

It would be a monumental task to type out all the numbers in Roman numerals. A better course of action is for you to type "Roman Numerals" into your web browser and you'll get many links to sites where they display them. You can even print them out for yourself.


Write a c program to print prime numbers from 1 to 10000 that has an unit digit which is multiple of 3?

This is a homework question and does not deserve an answer because you will learn nothing other than being lazy.


Is there a 10000 dollar bill?

They existed for a while, but aren't in print anymore.


Write a unix program to print print a pattern?

echo 'print a pattern'


Write an assembly language program to print a to z on screen?

write a program to print A to Z on screen in c?


Which is a program embedded in firmware that manages print jobs?

Print Server


How do you print from compaq laptop?

Use the option File / Print in the program you are using.


How do you print the answer you get?

Select the answer, copy and paste into a word processing program, and print as you would print any other document.


How to print a jet airways ticket?

If your screen does not give you an option to Print then save the document to a word program and print from there.


Q2 Write a program to print even numbers between 10 and 50?

You can use int i; for (i = 10; i &lt;= 50; i += 2) {//print i} as a program to print even numbers between 10 and 50.


How do you go to print preview?

It depends on the program, but usually it's in the "Print" dialogue box.