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.
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.
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;
}
}
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;
}