# Roman Numerals

This example converts the number 1788 to Roman numerals.

## Processor: ATtiny 2313

This example was compiled for the 2313 and may give incorrect results on other processors.*\
The calculation takes a bit over 6000 cycles and uses the following algorithm:

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

char roman_Number[10];
int i = 0;

int main(){
  long int number = 1788;

  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);
      }
    }
  }
  
  return 0;
}


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


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

The resulting string is stored in ram starting at location 0x60. The result can best be seen by looking in the 'abc' column of the memory panel. This code was taken from [cquestions.com][1]

*_For example, if run on the ATmega238, this location is in the I/O memory rather than sram. Values in I/O memory may be masked._

[1]: https://cquestions.com
