Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C Exercises: Print a table of all the Roman numeral equivalents of the decimal numbers in the range 1 to 50

C Basic Declarations and Expressions: Exercise-85 with Solution

Write a C program to print a table of all the Roman numeral equivalents of the decimal numbers in the range 1 to 50.

Sample Solution:

C Code:

#include<stdio.h>
int main()
{
int n;
printf("Decimal  Roman\n");
printf("numbers  numerals\n");
printf("-------------------\n");
for(int i=1; i<=100; i++)
{
n = i;
printf("  %d        ",i);
while(n != 0)
{
if (n >= 1000)
{
printf("M");
n -= 1000;
}
else if (n >= 900)
{
printf("CM");
n -= 900;
}
else if (n >= 500)
{
printf("D");
n -= 500;
}
else if (n >= 400)
{
printf("CD");
n -= 400;
}
else if (n >= 100)
{
printf("C");
n -= 100;
}
else if (n >= 90)
{
printf("XC");
n -= 90;
}
else if (n >= 50)
{
printf("L");
n -= 50;
}
else if (n >= 40)
{
printf("XL");
n -= 40;
}
else if (n >= 10)
{
printf("X");
n -= 10;
}
else if (n >= 9)
{
printf("IX");
n -= 9;
}
else if (n >= 5)
{
printf("V");
n -= 5;
}
else if (n >= 4)
{
printf("IV");
n -= 4;
}
else if (n >= 1)
{
printf("I");
n -= 1;
}
}
printf("\n");
}
return 0;
} 

Sample Output:

Decimal  Roman
numbers  numerals
-------------------
  1        I
  2        II
  3        III
  4        IV
  5        V
  6        VI
  7        VII
  8        VIII
  9        IX
  10        X
  11        XI
  12        XII
  13        XIII
  14        XIV
  15        XV
  16        XVI
  17        XVII
  18        XVIII
  19        XIX
  20        XX
  21        XXI
  22        XXII
  23        XXIII
  24        XXIV
  25        XXV
  26        XXVI
  27        XXVII
  28        XXVIII
  29        XXIX
  30        XXX
  31        XXXI
  32        XXXII
  33        XXXIII
  34        XXXIV
  35        XXXV
  36        XXXVI
  37        XXXVII
  38        XXXVIII
  39        XXXIX
  40        XL
  41        XLI
  42        XLII
  43        XLIII
  44        XLIV
  45        XLV
  46        XLVI
  47        XLVII
  48        XLVIII
  49        XLIX
  50        L
  51        LI
  52        LII
  53        LIII
  54        LIV
  55        LV
  56        LVI
  57        LVII
  58        LVIII
  59        LIX
  60        LX
  61        LXI
  62        LXII
  63        LXIII
  64        LXIV
  65        LXV
  66        LXVI
  67        LXVII
  68        LXVIII
  69        LXIX
  70        LXX
  71        LXXI
  72        LXXII
  73        LXXIII
  74        LXXIV
  75        LXXV
  76        LXXVI
  77        LXXVII
  78        LXXVIII
  79        LXXIX
  80        LXXX
  81        LXXXI
  82        LXXXII
  83        LXXXIII
  84        LXXXIV
  85        LXXXV
  86        LXXXVI
  87        LXXXVII
  88        LXXXVIII
  89        LXXXIX
  90        XC
  91        XCI
  92        XCII
  93        XCIII
  94        XCIV
  95        XCV
  96        XCVI
  97        XCVII
  98        XCVIII
  99        XCIX
  100        C

Flowchart:

C Programming Flowchart: Print a table of all the Roman numeral equivalents of the decimal numbers in the range 1 to 50.

C programming Code Editor:

Contribute your code and comments through Disqus.

Previous:Write a C program to calculate and print the average of some integers. Accept all the values preceding 888.
Next: Write a C program to display the sizes and ranges for each of C's data types.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Static variable inside of a function in C

The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().

The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.

The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().

Ref : https://bit.ly/3fOq7XP