Question

C++ Write code that will input a number and print the number, the square of the...

C++

  1. Write code that will input a number and print the number, the square of the number, and the cube of the number. Continue the operation until 999 is entered.
  2. Write code that will request the length and width of a rectangle until 0 is entered for either the length or the width. Print the area and perimeter for each set of inputs.
  3. What is the output for the following loop?

int number;

number = 1;

while (number < 11)

{

    number = number + 1;

    cout << number << endl;

}

12. What is the output for the following loop?

int number;

bool done;

number = 2;

done = FALSE;

while (!done)

{

    number = number * 2;

    if (number > 12)

        done = TRUE;

    cout << number << endl;

}

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

Code to Copy:

// Include the required header file.

#include <iostream>

using namespace std;

// Start of the main() function.

int main()

{

    

    // Declare an integer variable to store user input.

    int num;

    

    // Declare and initialize variables to hold the value

    // of square and cube of input number.

    int square=0, cube=0;

    

    // Prompt the user to input a number.

    cout << "\n Enter a number : ";

    cin >> num;

    // While loop to continues to execute until input

    // number is 999.

    while(num!=999)

    {

    

        // Display the number entered by the user.

        cout<< "\n Entered number is "<<num;

        // Calculate and display the square of a number.

        square=num*num;

        cout << "\n The square of the number "<<num<<" is "<<square;

        // Calculate and display the cube of a number

        cube=num*num*num;

        cout<< "\n The cube of the number "<<num<<" is "<<cube<<endl;

        // Prompt the user to continue the operation or

        // stops the operation by entering 999.

        cout<< "\n Enter a number [999 to exit] : ";

        cin >> num;

    // End of while loop

    }

    return 0;

// End of the main() function.  

}

Code to Copy:

// Include the required header file.

#include <iostream>

using namespace std;

// Start of the main() function.

int main()

{

    // Declare four integers variable to store the length,

    // width, area and perimeter of a rectangle.

    int len, width, area, perimeter;

    // Prompt the user to input the length and width of

    // a rectangle.

    cout<< "Enter the length and width of a rectangle: ";

    cin>>len>>width;

    // While loop continues to execute until 0 is entered

    // for either the length or the width.

    while(len!=0 && width!=0)

    {

        // Calculate and display the area of a rectangle.

        area=len*width;

        cout<< "Area of a rectangle : "<<area;

        // Calculate and display the perimeter of a

        // rectangle.

        perimeter=2*(len+width);

        cout<< "\nPerimeter of a rectangle : " <<perimeter <<endl;

        // Prompt the user to continue the operation or

        // stops by entering 0 for either length or width.

        cout<< "\nEnter the length and width of a rectangle [0 to exit]: ";

        cin>>len>>width;

    // End of while loop.

    }

    return 0;

// End of the main() function.

}

Add a comment
Know the answer?
Add Answer to:
C++ Write code that will input a number and print the number, the square of the...
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
  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then...

    Creating a Class in C++ Summary In this lab, you create a programmer-defined class and then use it in a C++ program. The program should create two Rectangle objects and find their area and perimeter. Instructions Ensure the class file named Rectangle.cpp is open in your editor. In the Rectangle class, create two private attributes named length and width. Bothlength and width should be data type double. Write public set methods to set the values for lengthand width. Write public...

  • C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class...

    C++ program. int main() {    Rectangle box;     // Define an instance of the Rectangle class    double rectWidth; // Local variable for width    double rectLength; // Local variable for length    string rectColor;    // Get the rectangle's width and length from the user.    cout << "This program will calculate the area of a\n";    cout << "rectangle. What is the width? ";    cin >> rectWidth;    cout << "What is the length? ";    cin >>...

  • 12. Write a C function void rect(int *ar, int *per, int len, int wid) that computes...

    12. Write a C function void rect(int *ar, int *per, int len, int wid) that computes the area ar and perimeter per of a rectangle with length len and width wid. Test it with a main program that inputs the length and width of a rectangle and outputs its area and perimeter. Output the value in the main program, not in the procedure. Sample Input 6   10 Sample Output Length: 6 Width: 10 Area: 60 Perimeter: 32

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • In the code (C++) below, if you input 2 as length and 4 as width, the...

    In the code (C++) below, if you input 2 as length and 4 as width, the output is: Enter the rectangle's length: 2 Enter the rectangle's width: 4 Length: 2 | Width: 4 | Area: 8 | Perimeter:12 We worked on this code during class, but there were some things that I did not understand. 1) how does compiler read"l" as length, "w" as width, etc.? I thought you had to update it first, like "w=width." 2) i thought you...

  • Required in C++ I'm asked to: Print a histogram in which the total number of times...

    Required in C++ I'm asked to: Print a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character like * that number of times. Below is my current code. I am not allowed to use arrays or anything too advanced as I just started the class. I would really appreciate the help as I've been stuck on it for a while now. I can only get it to print...

  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • This is a fill in the code type: // FILL IN THE CODE - Write a...

    This is a fill in the code type: // FILL IN THE CODE - Write a program to multiply 2 numbers, print out the results and print out the original numbers in ascending order. #include <iostream> using namespace std; int main() {   int num1;       // holds 1st number   int num2;       // holds 2nd number   int result;       // holds result of multiplication   int *num1Ptr = nullptr; // int pointer which will be set to point to the 1st number   int *num2Ptr...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

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
ADVERTISEMENT