C Program to Convert Numbers to Roman Numerals

C Program to Convert Numbers to Roman Numerals.

  1. #include <stdio.h>
  2.  
  3. void predigit(char num1, char num2);
  4. void postdigit(char c, int n);
  5.  
  6. char romanval[1000];
  7. int i = 0;
  8. int main()
  9. {
  10.     int j;
  11.     long number;
  12.  
  13.     printf("Enter the number: ");
  14.     scanf("%d", &number);
  15.     if (number <= 0)
  16.     {
  17.         printf("Invalid number");
  18.         return 0;
  19.     }
  20.     while (number != 0)
  21.     {
  22.         if (number >= 1000)
  23.         {
  24.             postdigit('M', number / 1000);
  25.             number = number - (number / 1000) * 1000;
  26.         }
  27.         else if (number >= 500)
  28.         {
  29.             if (number < (500 + 4 * 100))
  30.             {
  31.                 postdigit('D', number / 500);
  32.                 number = number - (number / 500) * 500;
  33.             }
  34.             else
  35.             {
  36.                 predigit('C','M');
  37.                 number = number - (1000-100);
  38.             }
  39.         }
  40.         else if (number >= 100)
  41.         {
  42.             if (number < (100 + 3 * 100)) 
  43.             {
  44.                 postdigit('C', number / 100);
  45.                 number = number - (number / 100) * 100;
  46.             }
  47.             else
  48.             {
  49.                 predigit('L', 'D');
  50.                 number = number - (500 - 100);
  51.             }
  52.         }
  53.         else if (number >= 50 )
  54.         {
  55.             if (number < (50 + 4 * 10))
  56.             {
  57.                 postdigit('L', number / 50);
  58.                 number = number - (number / 50) * 50;
  59.             }
  60.             else
  61.             {
  62.                 predigit('X','C');
  63.                 number = number - (100-10);
  64.             }
  65.         }
  66.         else if (number >= 10)
  67.         {
  68.             if (number < (10 + 3 * 10))
  69.             {
  70.                 postdigit('X', number / 10);
  71.                 number = number - (number / 10) * 10;
  72.             }
  73.             else
  74.             {
  75.                 predigit('X','L');
  76.                 number = number - (50 - 10);
  77.             }
  78.         }
  79.         else if (number >= 5)
  80.         {
  81.             if (number < (5 + 4 * 1))
  82.             {
  83.                 postdigit('V', number / 5);
  84.                 number = number - (number / 5) * 5;
  85.             }
  86.             else
  87.             {
  88.                 predigit('I', 'X');
  89.                 number = number - (10 - 1);
  90.             }
  91.         }
  92.         else if (number >= 1)
  93.         {
  94.             if (number < 4)
  95.             {
  96.                 postdigit('I', number / 1);
  97.                 number = number - (number / 1) * 1;
  98.             }
  99.             else
  100.             {
  101.                 predigit('I', 'V');
  102.                 number = number - (5 - 1);
  103.             }
  104.         }
  105.     }
  106.     printf("Roman number is: ");
  107.     for(j = 0; j < i; j++)
  108.         printf("%c", romanval[j]);
  109.     return 0;
  110. }
  111.  
  112. void predigit(char num1, char num2)
  113. {
  114.     romanval[i++] = num1;
  115.     romanval[i++] = num2;
  116. }
  117.  
  118. void postdigit(char c, int n)
  119. {
  120.     int j;
  121.     for (j = 0; j < n; j++)
  122.         romanval[i++] = c;
  123. }

 

Comments

Popular posts from this blog

C Program to Display the ATM Transaction

Java programming language

Python Program to Print all Prime Numbers in an Interval