-6

Given a natural number less or equal to 99, print it as a roman number.

Is there a method for this? How do I solve this assignment? Noob question here.

Topliner
  • 1
  • 3
  • That is too complicated, no way our teacher gave this much code to write. It's my first CS coding assignment. – Topliner Oct 08 '16 at 18:04
  • Copy "How to print roman numbers?" from the title of your question. Paste it into the StackOverflow search bar on the upper right. Et Voilà :) – Lorenzo Addazi Oct 08 '16 at 18:04
  • 2
    @Topliner also, this is a very classical first assignment. Not everything in life is easy. Some things, *most things*, will require you to put in thought and research of your own if you ever grow up. – Marcus Müller Oct 08 '16 at 18:05
  • I posted this question, because those threads were too much for a beginner in CS course. I thought there is an easier way to do it, like using a method. – Topliner Oct 08 '16 at 18:06
  • Roman numerals *are* complicated, that's why Hindu-Arabic numerals are so popular. There's nothing in the C standard library for this task, because it's not used that often in the applications for which C was intended. –  Oct 08 '16 at 18:09
  • 1
    those solutions really aren't hard to come up with. Some are more elegant than others, but a `while` loop containing a cascade of `if(num <...)` would definitely work. – Marcus Müller Oct 08 '16 at 18:09

2 Answers2

0

This code would work, it should be self explanatory.

#include <stdio.h>

void printRomanNumerals(int input);

int main(int argc, const char * argv[]) {
    printf("Input your number!\n");
    int i;
    scanf("%d", &i);
    printRomanNumerals(i);
    return 0;
}

void printRomanNumerals(int input) {

    switch (input / 10) {
        case 1:
            printf("%s","X");
            break;
        case 2:
            printf("%s","XX");
            break;
        case 3:
            printf("%s","xxx");
            break;
        case 4:
            printf("%s","XL");
            break;
        case 5:
            printf("%s","L");
            break;
        case 6:
            printf("%s","LX");
            break;
        case 7:
            printf("%s","LXX");
            break;
        case 8:
            printf("%s","LXXX");
            break;
        case 9:
            printf("%s","XC");
            break;
        default:
            break;
    }

    switch (input % 10) {
        case 1:
            printf("%s","I");
            break;
        case 2:
            printf("%s","II");
            break;
        case 3:
            printf("%s","III");
            break;
        case 4:
            printf("%s","IV");
            break;
        case 5:
            printf("%s","V");
            break;
        case 6:
            printf("%s","VI");
            break;
        case 7:
            printf("%s","VII");
            break;
        case 8:
            printf("%s","VII");
            break;
        case 9:
            printf("%s","IX");
            break;
        default:
            break;
    }

}
0

Some of the answers on those linked pages were way too complicated. Why do the noobs get all the fun problems...

#include <stdio.h>

const char *digits[] = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
const char *prefixes[] = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };

#define LIMIT sizeof(digits) / sizeof(char *) * sizeof(prefixes) / sizeof(char *)

int main() {
    int i = 0;

    do {
        printf("Da mihi numeralis (0 si velis relinquere): ");
        (void) scanf("%2i", &i);
        (void) fpurge(stdin);
        if (0 < i && i < LIMIT) {
            printf("%s%s\n", prefixes[i / 10], digits[i % 10]);
        }
    } while (i > 0);

    return 0;
}
cdlane
  • 40,441
  • 5
  • 32
  • 81