SET-151 C Programming #1 Homework: 7.5 Roman Numerals Just C not C++ please use
ID: 3726923 • Letter: S
Question
SET-151 C Programming #1
Homework: 7.5 Roman Numerals
Just C not C++
please use this format
Roman Numerals:
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
Use % for modulus division.
Step 1
Write a program that will and display 3 menu options. The options are:
Display the first 50 Roman Numerals.
Enter a number to be converted to Roman Numerals.
Exit.
Prompt the user for a selection: A, B or C.
If the user selects A then print 2 Columns of numbers on the screen. The leftmost column will display the roman numerals for numbers 1 – 25 and the rightmost column will display the roman numerals for number 26 – 50.
For example:
Number/Numeral Number/Numeral
1 = I 26 = XXVI
2 = II 27 = XXVII
3 = III 28 = XXVIII
4 = IV 29 = XXIX
5 = V 30 = XXX
.
.
.
25 = XXV 50 = L
Press any key to continue?
If the user selects B then prompt the user for a number between 1 and 3999 and then display the roman numeral. Only allow positive integers between 1 and 3999. Loop until a valid number is entered
If the user selects C then end the program.
Explanation / Answer
// ------------------------------------------------------------------------------------------
// Name: Your name here
// Class: C Programming
// Abstract: Homework 1.
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Includes
// ------------------------------------------------------------------------------------------
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
// ------------------------------------------------------------------------------------------
// Constants
// storing roman values of digits from 0-9
// when placed at different places
char* m[] = {"", "M", "MM", "MMM"};
char* c[] = {"", "C", "CC", "CCC", "CD", "D",
"DC", "DCC", "DCCC", "CM"};
char* x[] = {"", "X", "XX", "XXX", "XL", "L",
"LX", "LXX", "LXXX", "XC"};
char* i[] = {"", "I", "II", "III", "IV", "V",
"VI", "VII", "VIII", "IX"};
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Prototypes
void displayOptions();
void displayFirstFiftyRomanNumerals();
char* intToRoman(int num);
// ------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------
// Name: main
// Abstract: This is where the program starts
// ------------------------------------------------------------------------------------------
void main( )
{
char option, cont;
int num;
displayOptions();
scanf("%c", &option);
while(1){
if(option == 'A'){
displayFirstFiftyRomanNumerals();
displayOptions();
}else if(option == 'B'){
scanf("%d", &num);
while(num < 1 || num > 3999){
printf("Enter a valid number between 1 and 3999 ");
scanf("%d", &num);
}
printf("%d = %-10s ", num, intToRoman(num));
displayOptions();
}else if(option == 'C'){
break;
}
scanf("%c", &option);
}
return;
}
void displayOptions(){
printf(" Enter the following options: ");
printf("A: Display the first 50 Roman Numerals. ");
printf("B: Enter a number to be converted to Roman Numerals. ");
printf("C: Exit. ");
}
char* intToRoman(int num)
{
// Converting to roman
char* thousands = m[num/1000];
char* hundereds = c[(num%1000)/100];
char* tens = x[(num%100)/10];
char*>
// char* ans = thousands + hundereds + tens + ones;
char* ans = (char*) malloc(sizeof(char) * 20);
strcpy(ans, thousands);
strcat(ans, hundereds);
strcat(ans, tens);
strcat(ans, ones);
return ans;
}
void displayFirstFiftyRomanNumerals(){
int i;
printf("Number/Numeral Number/Numeral ");
for(i = 1; i <= 25; i++){
printf("%2d = %10s %2d = %10s ", i, intToRoman(i), i+25, intToRoman(i+25));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.