93.1k 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. 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 mgAfter 12 hours: 25.000000 mgAfter 18 hours: 12.500000 mgNote: A cup of coffee has about 100 mg. A soda has about 40 mg. An "energy" drink (a misnomer) has between 100 mg and 200 mg.

User Ogunsanya
by
7.9k points

2 Answers

1 vote

c++

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

double caffeineMg;

cin>>caffeineMg;

cout<<"After 6 hours: "<<fixed<<setprecision(2)<<caffeineMg/2.0<<" mg\\";

cout<<"After 12 hours: "<<fixed<<setprecision(2)<<caffeineMg/4.0<<" mg\\";

cout<<"After 24 hours: "<<fixed<<setprecision(2)<<caffeineMg/16.0<<" mg\\";

cin >> caffeineMg;

return 0;

}

User Qerub
by
8.9k points
1 vote

Answer:

// here is code in java.

import java.util.*;

class Solution

{

// main method of class

public static void main (String[] args) throws java.lang.Exception

{

try{

// declare variable

double caffeine;

// scanner object to read input from user

Scanner scr=new Scanner(System.in);

System.out.print("Enter the initial amount of caffeine:");

// read the initial amount of caffeine

caffeine=scr.nextDouble();

// calculate amount of caffeine after 6,12,18 hours

for(int i=1;i<=3;i++)

{

System.out.println("After "+ i*6+" hours:"+(caffeine/2)+" mg");

// update the caffeine after every 6 hours

caffeine=caffeine/2;

}

}catch(Exception ex){

return;}

}

}

Step-by-step explanation:

Create a variable "caffeine" to store the initial amount of caffeine given by user. Run a loop for three time, it will calculate the amount of caffeine left after every 6 hours.First it will give the amount of caffeine left after 6 hours, then caffeine left after 12 hours and in last caffeine after 18 hours.

Output:

Enter the initial amount of caffeine:100

After6 hours:50.0 mg

After12 hours:25.0 mg

After18 hours:12.5 mg

User Petr Stodulka
by
7.7k points