Answer to Question #64391 in C++ for yashfa

Question #64391
A fabulously wealthy but unthinking king agreed to give a beggar one cent and double the amount for 64 days in c++ by while loop.
1
Expert's answer
2017-01-04T04:21:36-0500
#include <iostream>
#include <vector>

int main()
{
int counter = 0;
std::vector<int> amount_of_money;
amount_of_money.push_back(1);
int buff = 0;
while (counter < 64) {
for (int i = 0; i < amount_of_money.size(); i++) {
amount_of_money[i] = amount_of_money[i] * 2 + buff;
if (amount_of_money[i] / 10 > 0) {
buff = amount_of_money[i] / 10;
amount_of_money[i] %= 10;
if (i + 1 == amount_of_money.size()) amount_of_money.resize(i + 2);
}
else buff = 0;
}
counter++;
}
std::cout << "After 64 days the beggar will have ";
for (int i = amount_of_money.size() - 1; i >= 0; i--)
std::cout << amount_of_money[i];
std::cout << " cents.\n\n";
system("pause");
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS