C program to Convert Decimal Number into Roman Number

C program to Convert Decimal Number into Roman Number

8463
9
SHARE

C program to Convert Decimal Number into Roman Number : Roman numerals, the numeric system in ancient Rome, uses combinations of letters from the Latin alphabet to signify values. There are several ways of representing the roman say 4 decimal number can be represented as IV or IIII. But as per standardization of Roman Numbers IV is valid and accepted by Mathematicians as better way to represent it.  Cost cutting is always being a fashion and been adapted by all, same happens in case of Roman Numbers. Lesser number of digits representation is considered as better.

The Romans used a special method of showing numbers, based on the following symbols:

 1 5 10 50 100 500 1000 I V X L C D M

.

Now there are few rules that are being followed by modern age representatives :

1. A number written in  Decimal numerals can be broken into digits. For example, 1903 is composed of 1, 9, 0, and 3. To write the Roman numeral, each of the non-zero digits should be treated separately. In the above example, 1,000 = M, 900 = CM, and 3 = III. Therefore, 1903 = MCMIII.

2. The symbols “I”, “X”, “C”, and “M” can be repeated three times in succession, but no more. (They may appear more than three times if they appear non-sequentially, such as XXXIX.) “D”, “L”, and “V” can never be repeated.

3. “I” can be subtracted from “V” and “X” only. “X” can be subtracted from “L” and “C” only. “C” can be subtracted from “D” and “M” only. “V”, “L”, and “D” can never be subtracted.

Numbers greater than 1,000 are formed by placing a dash over the symbol, meaning “times 1,000”, but these are not commonly used:

 5,000 10,000 50,000 100,000 500,000 1,000,000 _ V _ X _ L _ C _ D _ M

.

I have also avoided the same in the below . So there is one conflict in below program that it repeats M as many as times the thousands are there. I will update the modern Large Roman number logic in my next article about Roman number conversion.

By considering all above rule, let us see how to write a C program to convert Decimal number into Roman Number which take decimal number as input of the program and displays the equivalent Roman number.

Platform used : C Language

Compiler Used : Dev C++

C program to convert Decimal Number into Roman Number :

```#include<stdio.h>
#include<conio.h>
char roman_Number[10000];
int i=0;
void prefix(char c1,char c2){
roman_Number[i++] = c1;
roman_Number[i++] = c2;
}

void suffix(char c,int n){
int j;
for(j=0;j<n;j++)
roman_Number[i++] = c;

}

int main(){

int j;
long int number;

printf("Enter the number you wish to convert to ROMAN Number: ");
scanf("%d",&number);

if(number <= 0){
printf("Invalid number");
getch();
return 0;
}

while(number != 0){

if(number >= 1000){
suffix('M',number/1000);
number = number - (number/1000) * 1000;
}
else if(number >=500){
if(number < (500 + 4 * 100)){
suffix('D',number/500);
number = number - (number/500) * 500;
}
else{
prefix('C','M');
number = number - (1000-100);
}
}
else if(number >=100){
if(number < (100 + 3 * 100)){
suffix('C',number/100);
number = number - (number/100) * 100;
}
else{
prefix('L','D');
number = number - (500-100);
}
}
else if(number >=50){
if(number < (50 + 4 * 10)){
suffix('L',number/50);
number = number - (number/50) * 50;
}
else{
prefix('X','C');
number = number - (100-10);
}
}
else if(number >=10){
if(number < (10 + 3 * 10)){
suffix('X',number/10);
number = number - (number/10) * 10;
}
else{
prefix('X','L');
number = number - (50-10);
}
}
else if(number >=5){
if(number < (5 + 4 * 1)){
suffix('V',number/5);
number = number - (number/5) * 5;
}
else{
prefix('I','X');
number = number - (10-1);
}
}
else if(number >=1){
if(number < 4){
suffix('I',number/1);
number = number - (number/1) * 1;
}
else{
prefix('I','V');
number = number - (5-1);
}
}
}

printf("Equivalent Roman Numeral is : ");
for(j=0;j<i;j++)
printf("%c",roman_Number[j]);
getch();
return 0;

}```

We hope that you all enjoyed the Roman number concept and program. If you have any issues with C code for converting Decimal number to Roman number ask us in form of comments.

SHARE
Previous articleC Program of Library Management System | C Projects
Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com