A child's parents promised to give the child $10 on her 12th birthday and double the gift on every following birthday until the annual gift exceeded $1000. Write a C++ program to determine how old the child will be when the last amount is given and the total amount the child will have received.

Respuesta :

Answer:

#include <iostream>

using namespace std;

int main()

{

   int gift=10;

   int birthday=12;

   int sum=10;

   

   while(gift < 1000){

       gift=gift*2;

       birthday++;

       sum=sum+gift;

   }

   cout << "The last gift will be given to her at age: "<<birthday << endl;

   cout << "Total Amount collected over the years: " << sum << endl;

   return 0;

}

Explanation:

Three integer (Numbers that don't have decimal points) variables of are initialized the first is the "gift" while the second is the "birthday".

  • int gift=10;
  • int birthday=12;
  • int sum=10;

with the information provided the child is given $10 on her 12th birthday.

The conditional statement here is "until the annual gift exceeded $1000"

which means she'll stop getting the gift when her annual gift exceeds $1000 . I decided to use a while loop but a for loop can also be used.

  • while(gift < 1000)

so while the gift is less than $1000 she'll keep getting it every birthday .

The gift is doubled every birthday and one year is added to her age  

  • gift=gift*2;
  • birthday++;

In order to know the total amount the child will have received we have to introduce the sum variable.

  • sum=sum+gift;

which adds every gift the child receives.

The last session here signifies how old the child will be when the last amount is given and the total amount the child will have received.

  •  cout << "The last gift will be given to her at age: "<<birthday << endl;
  •   cout << "Total Amount collected over the years: " << sum << endl;

ACCESS MORE
EDU ACCESS