C Exercises: Convert a given integer to roman number
C Programming Practice: Exercise-6 with Solution
Write a C programming to convert a given integer to roman number.
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void num_to_char(char **num, int bit, int n)
{
int i;
char low, mid, high;
char *p = *num;
switch (n) {
case 2:
low = 'C';
mid = 'D';
high = 'M';
break;
case 1:
low = 'X';
mid = 'L';
high = 'C';
break;
case 0:
low = 'I';
mid = 'V';
high = 'X';
break;
}
if (bit > 0) {
switch (bit) {
case 1:
case 2:
case 3:
for (i = 0; i < bit; i++) {
*p++ = low;
}
break;
case 4:
*p++ = low;
*p++ = mid;
break;
case 5:
*p++ = mid;
break;
case 6:
case 7:
case 8:
*p++ = mid;
for (i = 5; i < bit; i++) {
*p++ = low;
}
break;
case 9:
*p++ = low;
*p++ = high;
break;
}
}
*num = p;
}
static char roman_numeral[64];
static char *int_to_roman(int num)
{
char *p = &roman_numeral[0];
int thousand_bit_num = num / 1000;
int hundred_bit_num = (num % 1000) / 100;
int ten_bit_num = (num % 100) / 10;
int one_bit_num = num % 10;
int i;
memset(roman_numeral, 0, sizeof(roman_numeral));
if (thousand_bit_num > 0) {
if (thousand_bit_num < 4) {
for (i = 0; i < thousand_bit_num; i++) {
*p++ = 'M';
}
}
}
num_to_char(&p, hundred_bit_num, 2);
num_to_char(&p, ten_bit_num, 1);
num_to_char(&p, one_bit_num, 0);
return roman_numeral;
}
int main(void)
{
int n = 12;
printf("Original integer: %d", n);
printf("\nRoman number of the said integer: %s", int_to_roman(n));
return 0;
}
Sample Output:
Original integer: 12 Roman number of the said integer: XII
Pictorial Presentation:
Flowchart:
C Programming Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C programming to reverse digits of a given a 32-bit signed integer.
Next: Write a C program to convert a given roman number to an integer.
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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework