Question

For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...

For this assignment, you will be creating a simple “Magic Number” program.

When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message:

NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class!

Note that "NAME" and "CSCI 1410" are what the user entered.

From the welcome screen you will enter a menu-based system that presents the user with the following menu:

Please select an operation:

a: Display an example

b: Try it out yourself!

c: Exit

Your system has to handle both lowercase and capital letter entries (e.g., a and A, b and B, c and C), and this entry will be stored in a char data type. The program won’t exit until the user selects the exit option. After the user selects an option and the operation completes, you will reprint the menu and let the user start over.

If the user chooses Display an example, you should do exactly the same procedures that you would do in b., but choose a number by yourself and print out each step and result to the screen.

Once a selection for Try it out yourself! is made, you will prompt the user for the three-digit number, whose first digit is greater than its last (e.g., 901, 200, 765, etc.) and store it in an integer variable. Here are the steps that your program will use to calculate the magic number:

1.Your program will take the integer input from the user (e.g., 901),

2.Reverse the input (i.e., 109),

3.Subtract the reversal from the original number (i.e., 901-109 = 792).

4.Finally, reverse the resulting number (i.e., 297),

5.Add it to its un-reversed form (i.e., 297 + 792), and

6.The program will output the final result (This is the "magic". If you did it right it will be 1089).

NOTE: The original number that the user enters must be of integer type (not three chars).

As each of the steps listed above completes, your program should display the following:

The number you entered is: DISPLAY NUMBER (e.g., 351)

The reversal of the input is: DISPLAY NUMBER (e.g., 153)

Subtraction of reversal from the original number is: DISPLAY NUMBER (e.g., 351 - 153 = 198)

Reversal of the resulting number is: DISPLAY NUMBER (e.g., 891)

Addition of the number to the un-reversed form is: DISPLAY NUMBER (e.g., 891 + 198 = 1089)

The final outcome is: 1089, which is our “Magic Number”

For this program, you will not be using functions, classes, or structures. Everything will be done inside of main. All data will be stored in primitive data types (such as int, char, string).

For the entire program, you don’t have to worry about building in error checking. The user will always enter valid information and make valid choices.

You are expected to fully comment your code including:

Homework Answers

Answer #1

The following code will provide the required functionality:

#include<iostream>
#include<cstdlib>
using namespace std;

//function prototype
void display_example();
void try_it_out();
int reverse_num(int num);

int main()
{
string name, cls;
char ch;
cout<<"Your name, please: ";
cin>>name;
cout<<"Enter your class: ";
cin>>cls;
cout<<name<<", welcome to your Magic Number program. I hope it helps you with your "<<cls<<" 1410 class!"<<endl;
while(1)
{
cout<<"Please select an operation:"<<endl;
cout<<"a: Display an example\n";
cout<<"b: Try it out yourself!\n";
cout<<"c: Exit\n";

//read the input
cin>>ch;
//switch condition
switch(ch)
{
case ('a'):
display_example();
break;
case ('b'):
try_it_out();
break;
case ('c'):
exit(1);
}//end of switch case
}//end of while loop
return 0;
}//end of main function

void try_it_out()
{
int num, rem, rev ,res, reverse_res, magic_number;
cout<<"Please enter the three digit number, whose first digit is greater than its last\n";
cin>>num;
cout<<"The number you entered is: "<<num<<endl;

//reverse the number
rev = reverse_num(num);
cout<<"The reversal of the input is: "<<rev<<endl;

//subtract reverse number from original number
res = num - rev;
cout<<"Subtraction of reversal from the original number is: "<<res<<endl;

//revers the result
reverse_res = reverse_num(res);
cout<<"Reversal of the resulting number is: "<<reverse_res<<endl;

//add the result and its reverse
magic_number = res + reverse_res;
cout<<"Addition of the number to the un-reversed form is: "<<magic_number<<endl;

cout<<"The final outcome is: 1089, which is our \"Magic Number\" "<<endl;

cout<<"This is the \"magic\". If you did it right it will be 1089"<<endl<<endl;

}//end of try_it_out function

void display_example()
{
int num = 902, rem, rev ,res, reverse_res, magic_number;
cout<<"Let us assume that number is 902\n";
//reverse the number
rev = reverse_num(num);
cout<<"The reversal of the input is: "<<rev<<endl;

//subtract reverse number from original number
res = num - rev;
cout<<"Subtraction of reversal from the original number is: "<<res<<endl;

//revers the result
reverse_res = reverse_num(res);
cout<<"Reversal of the resulting number is: "<<reverse_res<<endl;

//add the result and its reverse
magic_number = res + reverse_res;
cout<<"Addition of the number to the un-reversed form is: "<<magic_number<<endl;

cout<<"The final outcome is: 1089, which is our \"Magic Number\" "<<endl;

cout<<"This is the \"magic\". If you did it right it will be 1089"<<endl<<endl;

}//end of display_example function
int reverse_num(int num)
{
int rev =0 ,d, rem;
d = num;
while(d!=0)
{
rem = d%10;
rev = rev*10 + rem;
d = d/10;
}//end of while loop
return rev;
}//end of reverse function

Output:

Know the answer?
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
Similar Questions
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all...
Python: Simple Banking Application Project Solution: • Input file: The program starts with reading in all user information from a given input file. The input file contains information of a user in following order: username, first name, last name, password, account number and account balance. Information is separated with ‘|’. o username is a unique information, so no two users will have same username. Sample input file: Username eaglebank has password 123456, account number of BB12 and balance of $1000....
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...
Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message. The program will prompt the user to select one of the following menu options: 1. Enter and encrypt a message 2. View encrypted message 3. Decrypt and view the message (NOTE: password protected) 4. Exit If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will...
Use C++ to implement the following program about Prime Factorization of a Number. Do BOTH parts...
Use C++ to implement the following program about Prime Factorization of a Number. Do BOTH parts of the problem or you will lose points. Provide comments to explain each step. a. Write a function that takes as a parameter a positive integer and returns a list (array) of the prime factors of the given integer. For example, if the parameter is 20, you should return 2 2 5. b. Write a function that tests the above function by asking the...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
In Assignment 1, you created a program for Marshall’s Murals that prompts a user for the...
In Assignment 1, you created a program for Marshall’s Murals that prompts a user for the number of interior and exterior murals scheduled to be painted during the next month. The program computes the expected revenue for each type of mural when interior murals cost $500 each and exterior murals cost $750 each. In this assignment, you are going to design a GUI version using Visual Studio Form. The programs prompt a user for the number of interior and exterior...
Your C program will do the following : Must use at least 2 function prototypes &...
Your C program will do the following : Must use at least 2 function prototypes & definitions . You can also use repetitions , control structures . You re not allowed any type of global arrays, or global variables. You are only allowed to use 2 dimensional arrays. 1. In your main program, create a array of size 7 X 7. 2. Create a function that accepts the empty array. The function will initiate the to zero. Then, the function...
Your assignment is to implement a computer program in C++ to implement the below application. According...
Your assignment is to implement a computer program in C++ to implement the below application. According to Dummies.com the following algorithm determines the amount of paint you need to paint the walls of a four-sided room: 1. Add together the length of each wall. (For example, if each of the four walls are 14, 20, 14, 20 feet respectively then the total length is 14 + 20 + 14 + 20 = 68 feet) 2. Multiply the sum by the...
please can you make it simple. For example using scanner or hard coding when it is...
please can you make it simple. For example using scanner or hard coding when it is a good idea instead of arrays and that stuff.Please just make one program (or class) and explain step by step. Also it was given to me a txt.htm 1.- Write a client program and a server program to implement the following simplified HTTP protocol based on TCP service. Please make sure your program supports multiple clients. The webpage file CS3700.htm is provided. You may...
For this assignment you'll be creating an application that has the user input a subtotal, tax...
For this assignment you'll be creating an application that has the user input a subtotal, tax rate and tip percentage and then displays the sales tax, tip amount and the total. You'll use JQuery instead of the getElementByX functions AND you will display all messages on the page (no alert or prompt boxes) The starter file is based off of the Lab 2 sales_tax application. Feel free to borrow code from your lab solution but realize you will need to...