Help with C please!! Write a program to produce a table of decimal numbers and r
ID: 3789255 • Letter: H
Question
Help with C please!!
Write a program to produce a table of decimal numbers and roman numerals from 1 to 1000. The Romans used a special method of showing numbers based on the following symbols I, V, X, L, C, D and M representing 1, 5, 10, 50, 100, 500 and 1000 respectively. There are a few rules for roman numerals (a) To write a roman numeral each of the nonzero digits should be treated separately. For example, to write 651 = 500 + 100 + 50 + 1, D = 500, C = 100, L = 50, I = 1, we have DCLI. (b) The symbols I.X.C and M (symbols representing powers of 10) can be repeated at most 3 times in succession. D. L and V (symbols not representing powers of 10) can never be repeated. (c) I can be subtracted from V and X only. X can be subtracted from L and C only. C can be subtracted from D and M only. In other words, symbol representing 10* can be subtracted from symbol representing 5 * 10* and 10 * 10*. V, L and D can never be subtracted. In other words, symbols not representing powers of 10 can never be subtracted. Your program should produce a table of decimal numbers and corresponding roman numerals. As a starting point find how a decimal number can be represented using numbers which has a roman equivalent. Given a decimal number n find the values of a, b, c, d, e, f, y which satisfies n = a* 1000 + 6 * 500 + c * 100 + d * 50 + e * 10 + f * h + g * 1 This will give you a roman numeral representation which will be correct in most cases. For example, 752 = 500 + 2 * 100 + 50 + 2 * 1 can be converted into DC C LI I. Since we have 2*100 C repeats and since we have 2*1. I repeats. After that you can take care of the special cases where a symbol appears 4 or more times or a no repeating symbol appears two or more times. Your final program will have several if-else statements. Sample output for this assignment is as follows i ii iii iv v vi vii viiiExplanation / Answer
C Program :
/* C Program to Convert Decimal Numbers to Roman value representation */
#include <stdio.h>
void beforedigit(char num1, char num2);
void afterdigit(char c, int n);
int i=0;
char romanval[1000];
int main()
{
int j,l;
long number;
//Loop to repeat 1000 numbers
for(l=1;l<=1000;l++)
{
i = 0;
//process to convert each number to roman numerical
number=l;
if (number <= 0)
{
printf("Incorrect number");
return 0;
}
//number is checked many conditons using if else's until the value is not eual to zero
while (number != 0)
{
if (number >= 1000)
{
afterdigit('M', number / 1000);
number = number - (number / 1000) * 1000;
}
else if (number >= 500)
{
if (number < (500 + 4 * 100))
{
afterdigit('D', number / 500);
number = number - (number / 500) * 500;
}
else
{
beforedigit('C','M');
number = number - (1000-100);
}
}
else if (number >= 100)
{
if (number < (100 + 3 * 100))
{
afterdigit('C', number / 100);
number = number - (number / 100) * 100;
}
else
{
beforedigit('L', 'D');
number = number - (500 - 100);
}
}
else if (number >= 50 )
{
if (number < (50 + 4 * 10))
{
afterdigit('L', number / 50);
number = number - (number / 50) * 50;
}
else
{
beforedigit('X','C');
number = number - (100-10);
}
}
else if (number >= 10)
{
if (number < (10 + 3 * 10))
{
afterdigit('X', number / 10);
number = number - (number / 10) * 10;
}
else
{
beforedigit('X','L');
number = number - (50 - 10);
}
}
else if (number >= 5)
{
if (number < (5 + 4 * 1))
{
afterdigit('V', number / 5);
number = number - (number / 5) * 5;
}
else
{
beforedigit('I', 'X');
number = number - (10 - 1);
}
}
else if (number >= 1)
{
if (number < 4)
{
afterdigit('I', number / 1);
number = number - (number / 1) * 1;
}
else
{
beforedigit('I', 'V');
number = number - (5 - 1);
}
}
}
printf("Roman number is: %d ",l);
for(j = 0; j < i; j++)
printf("%c", romanval[j]);
printf(" ");
}
return 0;
}
//it gives the roman values part before digit
void beforedigit(char num1, char num2)
{
romanval[i++] = num1;
romanval[i++] = num2;
}
//it gives roman values part after digit
void afterdigit(char c, int n)
{
int j;
for (j = 0; j < n; j++)
romanval[i++] = c;
}
Output :
Roman number is: 1 I
Roman number is: 2 II
Roman number is: 3 III
Roman number is: 4 IV
Roman number is: 5 V
Roman number is: 6 VI
Roman number is: 7 VII
Roman number is: 8 VIII
Roman number is: 9 IX
Roman number is: 10 X
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.