5.6k views
3 votes
A half-life is the amount of time it takes for a substance or entity to fall to half its original value. Caffeine has a half-life of about 6 hours in humans. Given caffeine amount (in mg) as input, output the caffeine level after 6, 12, and 18 hours. Do not type your input into the optional input box below, rather, input in response to the prompts in the output terminal window (prompts are given to you in the program template). Use a string formatting expression with conversion specifiers to output the caffeine amount as floating-point numbers. Ex: If the input is 100, the output is: After 6 hours: 50.000000 mg After 12 hours: 25.000000 mg After 18 hours: 12.500000 mg

1 Answer

5 votes

Answer:

// program in C++.

#include <bits/stdc++.h>

using namespace std;

// main function

int main() {

// variable

float n;

cout<<"Enter the initial value:";

//Read the input from the user

cin>>n;

//Declare variables for storing values at 6, 12 and 18 hours

float hours_6, hours_12, hours_18;

//Set precision level of floating point number

cout << fixed;

cout << setprecision(6);

//calculate values at 6, 12 and 18 hours

hours_6 = n / 2.0;

hours_12 = hours_6 / 2.0;

hours_18 = hours_12 / 2.0;

//display the output

cout<<"After 6 hours: "<<hours_6<<"mg"<<endl;

cout<<"After 12 hours: "<<hours_12<<"mg"<<endl;

cout<<"After 18 hours: "<<hours_18<<" mg"<<endl;

return 0;

}

Step-by-step explanation:

Read an input from the user. Set the precision to 6 for displaying the result up to 6 decimal places. The first half life at 6 hours can be calculated by dividing the input by 2. The half life at 12 hours can be found out by dividing the half life value at 6 hours by 2. Similarly at 18 hours, we find the half life value by dividing the half life value at 12 hours by 2.

Output:

Enter the initial value:100

After 6 hours: 50.000000 mg

After 12 hours: 25.000000 mg

After 18 hours: 12.500000 mg

User Xizdaqrian
by
5.4k points