Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C programming
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
Edited 26-Aug-10 3:39am
Aescleal29.1K
v4
Comments
Peter_in_2780 at 24-Aug-10 19:34pm
   
OriginalGriff, here's one for you!
Luc Pattyn at 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 at 26-Aug-10 6:34am
   
Thank you for the suggestion Peter - I have created and posted the tip accordingly!
CPallini at 26-Aug-10 7:41am
   
Upper-case always: unless you're printing a book or are a musician. :-)
Aescleal at 26-Aug-10 9:40am
   
I binned the Cpp tag as the posters original solution was written in C.
Rate this: bad
good
Please Sign up or sign in to vote.

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 )
  Permalink  
Comments
aspdotnetdev at 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 at 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.
pradeep_bhadani at 25-Aug-10 8:06am
   
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 at 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: bad
good
Please Sign up or sign in to vote.

Solution 3

Since this comes up quite often, I have posted a complete solution as a Tip/Trick here[^]
  Permalink  
Comments
Christian Graus at 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 at 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 at 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 at 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: bad
good
Please Sign up or sign in to vote.

Solution 2

Yuck!
 
Sort out the presentation.
 

Also...
 

And this is such an important thing...
 
Do you understand how hard dates are?
  Permalink  
Comments
Christian Graus at 24-Aug-10 19:40pm
   
Dave, this is a comment, not an answer.
Niklas Lindquist at 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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 510
1 CPallini 330
2 OriginalGriff 225
3 George Jonsson 120
4 ChauhanAjay 104
0 OriginalGriff 6,474
1 Sergey Alexandrovich Kryukov 6,190
2 CPallini 5,180
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 26 Aug 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100