In: Computer Science
***C PROGRAMMING***
Roman Numerals:
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
Use % for modulus division.
Step 1
For example:
Number/Numeral Number/Numeral
1 = I 26 = XXVI
2 = II 27 = XXVII
3 = III 28 = XXVIII
4 = IV 29 = XXIX
5 = V 30 = XXX
.
.
.
25 = XXV 50 = L
Press any key to continue?
Code
#include <stdio.h>
void rom(int a) //prints roman value of the number
{
int n[] = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
char* r[] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
int i=12;
while(a>0)
{
int div = a/n[i];
a = a%n[i];
while(div--)
{
printf("%s",r[i]);
// strcat(res,r[i]);
}
i--;
}
printf("\n");
}
int main()
{
char ch;
while(1)
{
printf("A. Display the first 50 Roman Numerals.");
printf("\nB. Enter a number to be converted to Roman Numerals.");
printf("\nC. Exit");
printf("\nEnter A or B or C\n");
scanf(" %c",&ch);
if(ch=='C') //C to exit
break;
if(ch=='A') //A to print 50 roman valies
{
for (int i=1;i<=25;i++)
{
printf("%d=",i);
rom(i);
printf("\t\t\t\t%d=",i+25);
rom(i+25);
printf("\n");
}
}
if(ch=='B') //B to print desired value of number
{
printf("Enter number between 1 to 3999\n");
int n;
while(1)
{
scanf("%d",&n);
if(n<1 || n>3999)
{
printf("Enter number between 1 to 3999\n");
}
else
break;
}
rom(n);
}
}
return 0;
}
Terminal Work
.