Conversion of decimal to roman using C

In the previous post you learned to use for loop and do…while loop in program with the example for printing prime numbers. In this example, I will show you the use of while loop and if…else ladder. This program converts the decimal input to corresponding roman output. It seems difficult first time but once you have seen code it will be very easy to you. This program only converts number between 1 and 3000. Complete source code and output is given here.

Source Code

1: ///conversion of decimal to roman
  2: #include<stdio.h>
  3: #include<conio.h>
  4: 
  5: int main(){
  6:     int input;
  7:     int thousand,hundred,ten,one;
  8:     while(1){
  9:     printf("\nEnter the number between 1-3000: ");
 10:     scanf("%d",&input);
 11:     //printf("\n");
 12:     while(input<=0 || input>3000){
 13:         printf("Invalid input!");
 14:         printf("\nEnter the number again: ");
 15:         scanf("%d",&input);
 16:         //printf("\n");
 17:     }
 18: 
 19:     thousand = (input/1000)*1000;
 20:     hundred = ((input/100)%10)*100;
 21:     ten = ((input/10)%10)*10;
 22:     one = ((input/1)%10)*1;
 23: 
 24:     //checking for thousands
 25:     if(thousand==1000)
 26:         printf("M");
 27:     else if(thousand==2000)
 28:         printf("MM");
 29:     else if(thousand==3000)
 30:         printf("MMM");
 31: 
 32:     //checking for hundreds
 33:     if(hundred==100)
 34:         printf("C");
 35:     else if(hundred==200)
 36:         printf("CC");
 37:     else if(hundred==300)
 38:         printf("CCC");
 39:     else if(hundred==400)
 40:         printf("CD");
 41:     else if(hundred==500)
 42:         printf("D");
 43:     else if(hundred==600)
 44:         printf("DX");
 45:     else if(hundred==700)
 46:         printf("DXX");
 47:     else if(hundred==800)
 48:         printf("DXXX");
 49:     else if(hundred==900)
 50:         printf("CM");
 51: 
 52:     //checking for tens
 53:     if(ten==10)
 54:         printf("X");
 55:     else if(ten==20)
 56:         printf("XX");
 57:     else if(ten==30)
 58:         printf("XXX");
 59:     else if(ten==40)
 60:         printf("XL");
 61:     else if(ten==50)
 62:         printf("L");
 63:     else if(ten==60)
 64:         printf("LX");
 65:     else if(ten==70)
 66:         printf("LXX");
 67:     else if(ten==80)
 68:         printf("LXXX");
 69:     else if(ten==90)
 70:         printf("XC");
 71: 
 72:     //checking for ones
 73:     if(one==1)
 74:         printf("I");
 75:     else if(one==2)
 76:         printf("II");
 77:     else if(one==3)
 78:         printf("III");
 79:     else if(one==4)
 80:         printf("IV");
 81:     else if(one==5)
 82:         printf("V");
 83:     else if(one==6)
 84:         printf("VI");
 85:     else if(one==7)
 86:         printf("VII");
 87:     else if(one==8)
 88:         printf("VIII");
 89:     else if(one==9)
 90:         printf("IX");
 91: 
 92: 
 93:     printf("\nAnother conversion? ");
 94:     if(getche()=='n')
 95:     break;
 96:     }
 97: 
 98: 
 99:     return 0;
100: }

 

Output

 

roman

SHARE Conversion of decimal to roman using C

You may also like...

Leave a Reply

Your email address will not be published.

Share