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:
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:
Get Answers For Free
Most questions answered within 1 hours.