How to write Roman Numerals?
I, V, X, L, C, D, M are the seven characters used in Roman Numerals.
I - 1
V - 5
X - 10
L - 50
C - 100
D - 500
M - 1000
It uses combination of above characters to signify values. Below are the roman numerals for the numbers 1 to 10.
I, II, III, IV, V, VI, VII, VIII, IX, X
Basically, we are adding the numerals to signify a value.
For example, to represent the value 2
II => I + I =>1 + 1 => 2
III => I + I + I => 1 + 1 + 1 => 3
But, we cannot write any of the same roman numeral consecutively more than 3 times.
Example: We cannot write 4 as IIII or 9 as VIIII or 40 as XXXX
Instead, we need to write it as follows.
IV => V - I => 5 - 1 = 4 => 4 is written as IV
IX => X - I => 10 - 1 = 9 => 9 is written as IX
XL => L - X => 50 - 10 => 40 is written as XL
If the numeral with lower values precedes the numeral with higher value, we need to find the difference to get the value given roman numeral.
Write a C program to convert decimal number to roman equivalent.
I, V, X, L, C, D, M are the seven characters used in Roman Numerals.
I - 1
V - 5
X - 10
L - 50
C - 100
D - 500
M - 1000
It uses combination of above characters to signify values. Below are the roman numerals for the numbers 1 to 10.
I, II, III, IV, V, VI, VII, VIII, IX, X
Basically, we are adding the numerals to signify a value.
For example, to represent the value 2
II => I + I =>1 + 1 => 2
III => I + I + I => 1 + 1 + 1 => 3
But, we cannot write any of the same roman numeral consecutively more than 3 times.
Example: We cannot write 4 as IIII or 9 as VIIII or 40 as XXXX
Instead, we need to write it as follows.
IV => V - I => 5 - 1 = 4 => 4 is written as IV
IX => X - I => 10 - 1 = 9 => 9 is written as IX
XL => L - X => 50 - 10 => 40 is written as XL
If the numeral with lower values precedes the numeral with higher value, we need to find the difference to get the value given roman numeral.
Write a C program to convert decimal number to roman equivalent.
int main () {
int num;
printf("Enter your input:");
scanf("%d", &num);
printf("Roman Number: ");
while (num > 0) {
if (num >= 1000) {
/* M - 1000 */
printf("M");
num = num - 1000;
} else if (num >= 500) {
/*
* D is 500. CM is 900
* CM = M - C = 1000 - 100 => 900
*/
if (num >= 900) {
printf("CM");
num = num - 900;
} else {
printf("D");
num = num - 500;
}
} else if (num >= 100) {
/*
* C is 100. CD is 400
* CD = D - C = 500 - 100 => 400
*/
if (num >= 400) {
printf("CD");
num = num - 400;
} else {
printf("C");
num = num - 100;
}
} else if (num >= 50) {
/*
* L is 50. XC is 90
* XC = C - X = 100 - 10 => 90
*/
if (num >= 90) {
printf("XC");
num = num - 90;
} else {
printf("L");
num = num - 50;
}
} else if (num >= 9) {
/*
* XL is 40. IX is 9. X is 10
* XL = L - X = 50 - 10 = 40
* IX = X - I = 10 - 1 = 9
*/
if (num >= 40) {
printf("XL");
num = num - 40;
} else if (num == 9) {
printf("IX");
num = num - 9;
} else {
printf("X");
num = num - 10;
}
} else if (num >= 4) {
/*
* V is 5 and IV is 4
* IV = V - I = 5 - 1 => 4
*/
if (num >= 5) {
printf("V");
num = num - 5;
} else {
printf("IV");
num = num - 4;
}
} else {
printf("I");
num = num - 1;
}
}
printf("\n");
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input:999
Roman Number: CMXCIX
Enter your input:999
Roman Number: CMXCIX
#include
ReplyDeleteint main()
{
int n,a=1;
printf("entetr the num.");
do
{
scanf("%d",&n);
while(n>=1000 || n>=900)
{
if(n>=1000)
printf("M",n=n-1000);
else if(n>=900)
printf("CM",n=n-900);
}
while(n>=500 || n>=400)
{
if(n>=500)
printf("D",n=n-500);
else if(n>=400)
printf("CD",n=n-400);
}
while(n>=100 || n>=90)
{
if(n>=100)
printf("C",n=n-100);
else if(n>=40)
printf("XC",n=n-40);
}
while(n>=50 || n>=40)
{
if(n>=50)
printf("L",n=n-50);
else if(n>=40)
printf("XL",n=n-40);
}
while(n>=10 || n>=9)
{
if(n>=10)
printf("X",n=n-10);
else if(n>=9)
printf("IX",n=n-9);
}
while(n>=5 || n>=4)
{
if(n>=5)
printf("V",n=n-5);
else if(n>=4)
printf("IV",n=n-4);
}
while(n>=1)
printf("I",n=n-1);
scanf("%d",&a);
}
while(a!=0);
return 0;
}
How to represent 47 number
ReplyDeleteXLVII
Deletewhile(n>=100 || n>=90)
ReplyDelete{
if(n>=100)
printf("C",n=n-100);
else if(n>=40)
printf("XC",n=n-40)
check it please
Well written program...
ReplyDelete