C program to convert decimal to roman number





Convert numbers to roman numerals in c

#include<stdio.h>

void predigits(char c1,char c2);
void postdigits(char c,int n);

char roman_Number[1000];
int i=0;

int main(){

    int j;
    long int number;
   
    printf("Enter any natural number: ");
    scanf("%d",&number);
   
    if(number <= 0){
         printf("Invalid number");
         return 0;
    }

    while(number != 0){

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

    printf("Roman number will be: ");
    for(j=0;j<i;j++)
         printf("%c",roman_Number[j]);

    return 0;

}

void predigits(char c1,char c2){
    roman_Number[i++] = c1;
    roman_Number[i++] = c2;
}

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

Sample output:

Enter any natural number: 87
Roman number will be: LXXXVII





6. Write a c program to convert octal number to hexadecimal number.
8. Write a c program to convert hexadecimal number to octal number.
9. Write a c program to convert hexadecimal number to decimal number.
10. Write a c program to convert binary number to octal number.

10 comments:

Lostpoet said...

much better program with not so many conditionals.


---------------------------------------------------
#include
#include
#include
void reverse(char a[]);
void roman(int x,char a[],int i);
char values[4][3] = {{'I','V','X'},{'X','L','C'},{'C','D','M'},{'C'}};
main()
{
clrscr();
char final[20] = {'\0'};
int num,x,i=0;
printf("Enter the number that you want to convert to Roman Numerals:");
scanf("%d",&num);
if (num>5399)
num=0;
while(num)
{
x = num%10;
num /=10;
roman(x,final,i);
i++;
}
reverse(final);
printf("%s",&final);
getch();
return 1;
}
void roman(int x, char final[],int i)
{
int j=0,k=0,flag=0;
while (final[j]!='\0')
j++;
if(x<4)
{
for (k=0;k4)&&(x<9))
{
k=x-5;
final[j++]=values[i][1];
while (k)
{
final[j++]=values[i][0];
k--;
}
flag=1;
}
else if (x==9)
{
final[j++]=values[i][0];
final[j++]=values[i+1][0];
flag=1;
}
if(flag)
final[j++]='\0';
}
void reverse(char string[])
{
int j=0,k=0;
char temp[50];
while(string[j])
j++;
while(j)
temp[--j]=string[k++];
temp[k]='\0';
strcpy(string,temp);
}




------------------------------------------------

Lostpoet said...

the formatting of the website removed all the indentation.

Unknown said...

void dec2romanstr(int num){
int del[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
char * sym[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
char res[64] = "\0";
int i = 0;
while (num){
while (num/del[i]){
strcat(res, sym[i]);
num -= del[i];
}
i++;
}
puts(res);
}

Unknown said...

Sir this program is not working for the number: 495.....i dont know why this error s occuring and i m unable to debug ur program as well so i kindly request you to provide a correct program ........

kanishk said...

#include

int main ()
{int year,q,w,e,r,t,y,u,i,o;
printf("enter ur num");
scanf("%d",&year);
q=year/1000;
w=year%1000;
e=w/100;
r=w%100;
t=r/10;
y=r%10;
for(u=1;u<=q;u++)
printf("M");
if(e>=5)
{printf("D");
for(u=1;u<=(e-5);u++)
printf("C");
}
if(e<5)
{
for(u=1;u<=e;u++)
printf("C");
}
if(t>=5)
{printf("L");
for(u=1;u<=(t-5);u++)
printf("X");
}
if(t<5)
{
for(u=1;u<=t;u++)
printf("X");
}
if(y>=5)
{printf("V");
for(u=1;u<=(y-5);u++)
printf("I");
}
if(y<5)
{
for(u=1;u<=y;u++)
printf("I");
}
return 0;



}

kanishk said...

#include

int main ()
{int year,q,w,e,r,t,y,u,i,o;
printf("enter ur num");
scanf("%d",&year);
q=year/1000;
w=year%1000;
e=w/100;
r=w%100;
t=r/10;
y=r%10;
for(u=1;u<=q;u++)
printf("M");
if(e>=5)
{printf("D");
for(u=1;u<=(e-5);u++)
printf("C");
}
if(e<5)
{
for(u=1;u<=e;u++)
printf("C");
}
if(t>=5)
{printf("L");
for(u=1;u<=(t-5);u++)
printf("X");
}
if(t<5)
{
for(u=1;u<=t;u++)
printf("X");
}
if(y>=5)
{printf("V");
for(u=1;u<=(y-5);u++)
printf("I");
}
if(y<5)
{
for(u=1;u<=y;u++)
printf("I");
}
return 0;



}

Unknown said...

you have to change the part:

predigits('L','D');
number = number - (500-100);

TO
predigits('C','D');
number = number - (500-100);

Unknown said...

right...

Anonymous said...

lol
check the numebr 499
the output will be ldxcix
which is the output for 549

Anonymous said...
This comment has been removed by the author.