12,457,259 members (54,897 online)
Rate this:
See more:
I have to write a program to convert given year (in decimal form) into Roman numeral representation. These are the Roman numeral digits:

```Decimal 1  5  10  50  100  500  1000
Roman   i  v  x   l   c    d    m```

Some example conversions:
```Roman equivalent of 1988 is mdccclxxxviii.
Roman equivalent of 1525 is mdxxv.```

My program so far:
```#include<stdio.h>
#include<conio.h>

int main(){
clrscr();
int yr,i,j=0;
char roman[20];

printf("Enter Year : ");
scanf("%d",&yr);

if(yr/1000>=0)
{
for(i=0;i<(yr/1000);i++)
{
roman[j]='m';
j++;
}
yr = yr - (i+1)*1000;
}
if(yr/500>=0)
{
for(i=0;i<(yr/500);i++)
{
roman[j]='d';
j++;
}
yr = yr - (i+1)*500;
}
if(yr/100>=0)
{
for(i=0;i<(yr/100);i++)
{
roman[j]='c';
j++;
}
yr = yr - (i+1)*100;
}
if(yr/50>=0)
{
for(i=0;i<(yr/50);i++)
{
roman[j]='l';
j++;
}
yr = yr - (i+1)*50;
}
if(yr/10>=0)
{
for(i=0;i<(yr/10);i++)
{
roman[j]='x';
j++;
}
yr = yr - (i+1)*10;
}
if(yr/5>=0)
{
for(i=0;i<(yr/5);i++)
{
roman[j]='v';
j++;
}
yr = yr - (i+1)*5;
}
if(yr/1>=0)
{
for(i=0;i<(yr/1);i++)
{
roman[j]='i';
j++;
}
}
printf("year in roman : ");
for(i=0;i<=j;i++)
printf("%c",roman[i]);
getch();
}
```

But it only prints "m". Can anyone help me to sort out this problem?
Posted 24-Aug-10 13:14pm
Updated 26-Aug-10 3:39am
Aescleal33.2K
v4
Peter_in_2780 24-Aug-10 19:34pm

OriginalGriff, here's one for you!
Luc Pattyn 24-Aug-10 20:01pm

I object. It's unacceptable. Roman numbers use upper-case. always. no exceptions. none whatsoever.
I need a completely-disgusted icon here. In fact an entire line of them.
OriginalGriff 26-Aug-10 6:34am

Thank you for the suggestion Peter - I have created and posted the tip accordingly!
CPallini 26-Aug-10 7:41am

Upper-case always: unless you're printing a book or are a musician. :-)
Aescleal 26-Aug-10 9:40am

I binned the Cpp tag as the posters original solution was written in C.

Rate this:

## Solution 1

Hmmm.. do you know how to use the debugger ?

yr = yr - (i+1)*1000;

This is your problem, because you use i+1, yr becomes negative and so the rest of the tests do not work. You can set breakpoints in the IDE and step through your code to work out problems like this, that's how I solved it for you ( although I was suspicious from the start that this was the error )
aspdotnetdev 24-Aug-10 19:52pm

Reason for my vote of 5
Sounds correct. Explains the problem. Informs OP of better technique to solve problems. Answered before I could even finish formatting the question. 5.
Dalek Dave 24-Aug-10 20:05pm

Fair comment, but dates are notriously difficult.

It is a case of turning the date into a string and then manipulating the string.

I wrote something similat many years ago in ZX Basic and it was over 100 lines long. It is a horrendous thing to do.

how yr is negative
for ex yr = 1988
then
yr = 1988 - (0+1)*1000 = 988
yr = 988 - (0+1)*500 = 488
yr = 488 - (3+1)*100 = 88
yr = 88 - (0+1)*50 = 38
yr = 38 - (2+1)*10 = 8
yr = 8 - (0 + 1)*5 = 3
yr = 3 - (2+1)*3 = 0
Christian Graus 26-Aug-10 6:37am

If you knew how to use a debugger, you'd see for yourself. Do you not understand what a for loop does ? You defined i outside the loop, so it keeps it's value from the loop. If it's 1988, then it's value is 1, 1+1 = 2, therefore 1988 - 2000 is.....
Rate this:

## Solution 3

Since this comes up quite often, I have posted a complete solution as a Tip/Trick here[^]
Christian Graus 26-Aug-10 6:37am

I wish you wouldn't have. This is plainly a homework assignment, no-one NEEDS this. Everyone who gets this homework, even this guy who can't follow a basic for loop or use a debugger, will now turn in your code.
Dalek Dave 26-Aug-10 6:51am

Good hint, but as CG says, this is an oft asked homework thing.

Every week we see half a dozen indian programmers asking more or less the same question.
CPallini 26-Aug-10 7:32am

I don't agree with Christian & Dave. "Don't perform their homework" doesn't look a strong enough point in order to delete a possibly nice piece of code. I hope you'll restore the broken link.
Niklas Lindquist 26-Aug-10 9:02am

In favor of Pallinis comment, the ones asking homework questions don't know how to use search engines, so they won't find your tip/trick anyway. In favor of the comments from CG and DD, this guy could be your next colleague. Or more importantly, mine.
Rate this:

## Solution 2

Yuck!

Sort out the presentation.

Also...

And this is such an important thing...

Do you understand how hard dates are?
Christian Graus 24-Aug-10 19:40pm

Dave, this is a comment, not an answer.
Niklas Lindquist 25-Aug-10 2:36am

Well Dave, if you find dates hard, you could transform this problem into another domain, say, representing the number of peas in your soup using roman notation, then transform it back again. That way you will avoid having to deal with dates.

Top Experts
Last 24hrsThis month
 OriginalGriff 288 Maciej Los 258 Dave Kreskowiak 148 Mehdi Gholam 145 ppolymorphe 141
 OriginalGriff 6,916 ppolymorphe 4,101 Maciej Los 3,943 Karthik Bangalore 3,665 Richard Deeming 2,915