Question: #include using namespace std; int main() { char ans; int num; cout < < endl < < Do you want to square a number?(y/n) <
#include
using namespace std;
int main()
{
char ans;
int num;
cout << endl << "Do you want to square a number?(y/n)" << endl;
cin >> ans;//1 - START
while ((ans == 'y')||(ans == 'Y')) //2 - TEST
{
cout << "Enter number to be squared: "<< endl;
cin >> num;
cout << "The square of " << num << " is " << num*num << endl;
cout << endl << "Do you want to do another square?(y/n)" << endl;
cin >> ans;
}
return 0;
}
- Type in the above program as square.cpp. Add a comment to include your names and date. Compile and run with different values.
- Include a line that prints "Good bye!" before the program exits. This should only print once.
- What symbol does || stand for in C++?
- What happens if you answer "z" when asked if you want to square a number? Why?
- Summarize how the above loop works, for example _____ and _____ cause the loop to continue, __________ causes the loop to end.
- Modify the program to change the || to &&. What is the result? Why? Change it back when you have answered the question
- Comment out (put // at the front of the line) the line that is labeled 1 or START. What happens (is there an error?)? Why? Restore the lines, or un-comment, by removing the // after you have answered the question.
- Comment out the line "cin >> ans" near the end of the program. What happens, why? Un-comment the lines after you have answered the question.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
