Assessment question to test what you learned from your assignments. It is worth
ID: 3872544 • Letter: A
Question
Assessment question to test what you learned from your assignments. It is worth 1 point in total (partial credit will be given). Their grade will not affect your test grade but your assignments grades. For more details, review the "1380rubric.doc document with the Grading Criteria (available from Miscellaneous). Write a short Crprogram (include header files) that performs the following tasks: //insert header files //1. Declares a named constant called HALF that holds value 1/2 in it. // 2. Declares a variable named num2 and another named num1 that hold real numbers. //3. Prompts the user to "Enter a number and I will show its half: // 4. Reads the value from the keyboard and stores it in num1. //5. Calculates the half of num1 (using the named constant) and stores the result in num2 // 6. Formats the output to display the numbers in fixed format with only one decimal digit. // 7. Prints a message like the one below (N2 and N1 are the values corresponding to num2 and num1 respectively. N2, " is one half of ", N1 IMPORTANT: .you must choose the best data type for the named constant and variables so your answer is not truncated. For example, if the user enters 3, your program should display: 1.5 is one half of 3.0 .Be careful with the value assigned to HALF. . Use the named constant in step 5.Explanation / Answer
Given below is the code for the question. Please rate the answer if it helped. Thank you.
#include <iostream>
#include <iomanip> //for setprecision
using namespace std;
int main()
{
const double HALF = 1.0 / 2; //declare named constant HALF, using 1.0 instead of 1 to avoid integer division
double num1, num2; //variable for real numbers
//prompt the user
cout << "Enter a number and I will show its half: ";
//read the value from keyboard
cin >> num1;
num2 = HALF * num1; //calcualte half of num1 and store in num2
//change the settings to display decimal point and 1 digit after point
cout << fixed << setprecision(1);
cout << num2 << " is one half of " << num1 << endl;
}
output
Enter a number and I will show its half: 3
1.5 is one half of 3.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.