C Language | Decimal-to-Roman Conversion

Friday 14 December 2012

Centuries ago, people were using the Roman Numerals as their numbers. Today, we are using the Decimal numbers. Before, roman numerals is a specific letter that has an equivalent value. For example, capital letter I indicates that its value is one (1). Though we are not really using roman numerals today, we are still studying about this because this is one of the important history and foundations in mathematics.

Today's generation, there a a lot of kids that have difficulties in studying the roman numerals especially that it is composed of letters. So, there are a lot of confusions going on in the process of learning. Because of this, I made a program that can convert decimal numbers to roman numerals to make it more easy for the kids to learn. This program will also expand the knowledge of the future programmers about decision making statements and repetitive statements. Decision making statement is what we call Switch case statements. While, repetitive statements are the Do-While and For Loop statements. For this program we will be using for loop statement.  To start, check out the codes below.


   9:  #include <stdio.h>
  10:   
  11:  #include <conio.h>
  12:   
  13:   
  14:   
  15:  main()
  16:   
  17:  {
  18:   
  19:   
  20:   
  21:  int count;
  22:   
  23:  int b;
  24:   
  25:   
  26:   
  27:  int num=1;
  28:   
  29:  int num1,num2,num3,num4;
  30:   
  31:        
  32:   
  33:        printf("Enter how many counts: ");
  34:   
  35:        scanf("%d",&count);
  36:   
  37:        
  38:   
  39:        if(count>20) {
  40:   
  41:        printf("\nYou have exceeded the maximum input of 30");
  42:   
  43:        getch();
  44:   
  45:        return 0;
  46:   
  47:        }
  48:   
  49:        
  50:   
  51:        for(b=1;b<=count;b++) {
  52:   
  53:        printf("\nEnter Numbers: ");
  54:   
  55:        scanf("%d", &num);
  56:   
  57:        
  58:   
  59:  num1=num/1000 % 10;
  60:   
  61:  num2=num/100 % 10;
  62:   
  63:  num3=num/10 % 10;
  64:   
  65:  num4=num/1 % 10;
  66:   
  67:   
  68:   
  69:   
  70:   
  71:   
  72:   
  73:  switch(num1){
  74:   
  75:  case 1:
  76:   
  77:  printf(" M") ;
  78:   
  79:  break;
  80:   
  81:   
  82:   
  83:  case 2:
  84:   
  85:  printf(" MM");
  86:   
  87:  break;
  88:   
  89:   
  90:   
  91:  case 3:
  92:   
  93:  printf(" MMM");
  94:   
  95:  break;
  96:   
  97:   
  98:   
  99:  }
 100:   
 101:   
 102:   
 103:  switch(num2){
 104:   
 105:  case 1:
 106:   
 107:  printf("C");
 108:   
 109:  break;
 110:   
 111:   
 112:   
 113:  case 2:
 114:   
 115:  printf("CC");
 116:   
 117:  break;
 118:   
 119:   
 120:   
 121:  case 3:
 122:   
 123:  printf("CCC");
 124:   
 125:  break;
 126:   
 127:   
 128:   
 129:  case 4:
 130:   
 131:  printf("CD") ;
 132:   
 133:  break;
 134:   
 135:   
 136:   
 137:  case 5:
 138:   
 139:  printf("D");
 140:   
 141:  break;
 142:   
 143:   
 144:   
 145:  case 6:
 146:   
 147:  printf("DC");
 148:   
 149:  break;
 150:   
 151:   
 152:   
 153:  case 7:
 154:   
 155:  printf("DCC");
 156:   
 157:  break;
 158:   
 159:   
 160:   
 161:  case 8:
 162:   
 163:  printf("DCC");
 164:   
 165:  break;
 166:   
 167:   
 168:   
 169:  case 9:
 170:   
 171:  printf("CM");
 172:   
 173:   
 174:   
 175:  }
 176:   
 177:   
 178:   
 179:  switch(num3){
 180:   
 181:  case 1:
 182:   
 183:  printf("X");
 184:   
 185:  break;
 186:   
 187:   
 188:   
 189:  case 2:
 190:   
 191:  printf("XX");
 192:   
 193:  break;
 194:   
 195:   
 196:   
 197:  case 3:
 198:   
 199:  printf("XXX");
 200:   
 201:  break;
 202:   
 203:   
 204:   
 205:  case 4:
 206:   
 207:  printf("XL");
 208:   
 209:  break;
 210:   
 211:   
 212:   
 213:  case 5:
 214:   
 215:  printf("L");
 216:   
 217:  break;
 218:   
 219:   
 220:   
 221:  case 6:
 222:   
 223:  printf("LX");
 224:   
 225:  break;
 226:   
 227:   
 228:   
 229:  case 7:
 230:   
 231:  printf("LXX");
 232:   
 233:  break;
 234:   
 235:   
 236:   
 237:  case 8:
 238:   
 239:  printf("LXX");
 240:   
 241:  break;
 242:   
 243:   
 244:   
 245:  case 9:
 246:   
 247:  printf("XC");
 248:   
 249:  break;
 250:   
 251:   
 252:   
 253:  }
 254:   
 255:   
 256:   
 257:  switch(num4){
 258:   
 259:  case 1:
 260:   
 261:  printf("I") ;
 262:   
 263:  break;
 264:   
 265:   
 266:   
 267:  case 2:
 268:   
 269:  printf("II") ;
 270:   
 271:  break;
 272:   
 273:   
 274:   
 275:  case 3:
 276:   
 277:  printf("III") ;
 278:   
 279:  break;
 280:   
 281:   
 282:   
 283:  case 4:
 284:   
 285:  printf("IV") ;
 286:   
 287:  break;
 288:   
 289:   
 290:   
 291:  case 5:
 292:   
 293:  printf("V") ;
 294:   
 295:  break;
 296:   
 297:   
 298:   
 299:  case 6:
 300:   
 301:  printf("VI") ;
 302:   
 303:  break;
 304:   
 305:   
 306:   
 307:  case 7:
 308:   
 309:  printf("VII") ;
 310:   
 311:  break;
 312:   
 313:   
 314:   
 315:  case 8:
 316:   
 317:  printf("VIII") ;
 318:   
 319:  break;
 320:   
 321:   
 322:   
 323:  case 9:
 324:   
 325:  printf("IX") ;
 326:   
 327:  break;
 328:   
 329:   
 330:   
 331:  }
 332:   
 333:  }
 334:   
 335:  }


The program operates like this. Basically, the program is created to convert decimal to roman, that means the user need to input any numbers he want to convert. Before that, the user must enter an input in how many times he want to convert a decimal. This will stand as his final count for the for loop statement. The loop statement is responsible for asking the user to enter a number and how many times he want to put an input. After that, the entered number will be evaluated inside the loop. If the user entered five (5), the loop will then ask the user Enter Numbers five times. The inputs will be stored in a memory. After that, it will jump to the switch case statements and will look which case it will satisfy. Then it will show you the results. That will be the end of the program. You can check out the sample output below.





Hope you learned something. We hope to hear from you. Thanks! 

No comments:

Post a Comment

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...