Question

I am writing C++ code, and I need help for this. Create a program that will...

I am writing C++ code, and I need help for this.

Create a program that will convert a decimal value into Roman numeral, and output the result. The program will ask for an integer input, assume all inputs are valid, and the program should not end unless the user tells you to.

C++ code.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER: Here I am giving you the code and output if you have problem then comment or like it .

CODE:

#include <iostream>
using namespace std;
void predigit(char num1, char num2);
void postdigit(char c, int n);

char romanval[1000];
int i = 0;
int main()
{
int j;
long number;

while(1){
cout<<" Enter the number or enter 0 or negative number to exit: ";
cin>>number;

if (number <= 0)
{
cout<<"Invalid number";
return 0;
}
while (number != 0)
{
if (number >= 1000)
{
postdigit('M', number / 1000);
number = number - (number / 1000) * 1000;
}
else if (number >= 500)
{
if (number < (500 + 4 * 100))
{
postdigit('D', number / 500);
number = number - (number / 500) * 500;
}
else
{
predigit('C','M');
number = number - (1000-100);
}
}
else if (number >= 100)
{
if (number < (100 + 3 * 100))
{
postdigit('C', number / 100);
number = number - (number / 100) * 100;
}
else
{
predigit('L', 'D');
number = number - (500 - 100);
}
}
else if (number >= 50 )
{
if (number < (50 + 4 * 10))
{
postdigit('L', number / 50);
number = number - (number / 50) * 50;
}
else
{
predigit('X','C');
number = number - (100-10);
}
}
else if (number >= 10)
{
if (number < (10 + 3 * 10))
{
postdigit('X', number / 10);
number = number - (number / 10) * 10;
}
else
{
predigit('X','L');
number = number - (50 - 10);
}
}
else if (number >= 5)
{
if (number < (5 + 4 * 1))
{
postdigit('V', number / 5);
number = number - (number / 5) * 5;
}
else
{
predigit('I', 'X');
number = number - (10 - 1);
}
}
else if (number >= 1)
{
if (number < 4)
{
postdigit('I', number / 1);
number = number - (number / 1) * 1;
}
else
{
predigit('I', 'V');
number = number - (5 - 1);
}
}
}
cout<<"Roman number is: ";
for(j = 0; j < i; j++)
cout<< romanval[j];
}
  
return 0;
}

void predigit(char num1, char num2)
{
romanval[i++] = num1;
romanval[i++] = num2;
}

void postdigit(char c, int n)
{
int j;
for (j = 0; j < n; j++)
romanval[i++] = c;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
I am writing C++ code, and I need help for this. Create a program that will...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help writing a C programming code. I am trying to create a program that...

    I need help writing a C programming code. I am trying to create a program that returns ANY given number's decimal value as an Integer (Whole number). Examples: User input: 0.35 Output: 35 User input: 1.465 Output: 465 User input: 10.6054 Output: 6054

  • C++ programming language , I need help writing the code . Please explain if you can...

    C++ programming language , I need help writing the code . Please explain if you can . Exercise 10: Unit conversion (10 points) Write a console program that converts meters into feet and inches. The program starts by asking the user for a measurement in meters. The program should accept a floating point number. After getting the number, the program computes the number of feet and inches equal to the specified number of meters. The program outputs feet and inches...

  • I need help with this C++ assignment. Create a program which will implement the sum of...

    I need help with this C++ assignment. Create a program which will implement the sum of the numbers between 1 and n where n is an integer. When completed, the program will allow the user to input a positive integer (n) then the program will add all numbers between 1 and n (including n) and output the sum. For example, if the user enters 8 then the program will sum the numbers 1 through 8 and will output the sum....

  • Hello. I just need help with my c++ code. Write a program that: Creates an integer...

    Hello. I just need help with my c++ code. Write a program that: Creates an integer variable AND an integer pointer variable Ask the user to input an integer value Store the value in the integer variable Print the address of the variable. Make the pointer variable point at the integer value Print the value pointed to by the pointer Print the address of the pointer

  • Create a program that converts an integer to the specified base. in Matlab ----The program should...

    Create a program that converts an integer to the specified base. in Matlab ----The program should ask for 3 inputs. The number to convert. The base the number is in. And the base to convert the number to. ----The program should accept a base that is in the range of 2 to 16 inclusive. ----Display the result to the user and ask if they want to exit or convert another number.  Sub-goals: o Do not display leading zero's in...

  • Help on writing a MATLAB code. Let's say i am given a .txt file and in...

    Help on writing a MATLAB code. Let's say i am given a .txt file and in that text file there are 26 rows that represent individual students grades and then 7 columns that represent specific homework assignments. We want to ask the user to select a specific homework assignment and output the mean and standard deviation for that assignment. Then we want to ask the user to select an individual student and output the mean and standard deviation for that...

  • I have this c++ program that i have to create but i am stuck on it....

    I have this c++ program that i have to create but i am stuck on it. Write a height conversion program that shall allow user to convert from feet and inches to meters (option 1) and vice versa (option 2). User shall be able to specify the type of conversion (a menu of two options). Display an error message if an invalid option is specified. Otherwise, the program would read in a length in feet and inches (two separate integer...

  • I am writing a Python program to serve as a fitness tracker. I would love some...

    I am writing a Python program to serve as a fitness tracker. I would love some input as to how you would code this. I've included what I want this program to accomplish, what I will keep track of, and below all of that I have posted the assignment guidelines. Any suggestions on how to make this better/ more efficient would be welcome! I'm thinking of making a weekly tracker that takes input per day, and outputs at the end...

  • Write a program in C++ that converts a number entered in Roman numerals to a positive...

    Write a program in C++ that converts a number entered in Roman numerals to a positive integer. Your program should consist of a class, say, romanType. An object of type romanType should do the following: Store the number as a Roman numeral. Convert and store the number as a positive integer. Print the number as a Roman numeral or positive integer as requested by the user. The integer values of the Roman numerals are: M = 1000; D = 500;...

  • I need help getting my program to reject a negative input from the user. Here is...

    I need help getting my program to reject a negative input from the user. Here is what I have: #short description of what the program will do print("Hello, this program will ask you to enter a non-negative decimal integer. The program will then show the binary equivalent of the integer you choose.") #ask the user to enter the non-negative decimal integer number= int(input("Enter a non-negative decimal integer: ")) while number< 0: print("Number Cannot Be Negative!! Try again...") #create a variable...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT