Tomas Ambrulaitis - 1 month ago 6
C++ Question

# How to solve this for-loop exercise in c++ (beginner). n + nx2 + nx2x2 +

I was trying to write a c++ programm, which begins with int a - 5 items, and every second int b - gets bigger 2x times. For example 1second prints - 5, 2second - 10, 3second - 20. And then print a sum of it. (35). Since I'm a very beginner, got stuck with it.

``````int main()

{
int a,b;
cout << " Enter a and b: " << endl;
cin >> a >> b;
for (int i=1; i<=b; i++) {
cout << i << endl;
}
return 0;
}
``````

This after cin 3 seconds prints 1,2,3. I tried to sum it with +=i, but i do not get how does it work. How to write a code which tells to count
n + nx2 + nx2x2 + nx2x2x2.... in a loop.

Thank you!

``````#include <iostream>
using namespace std;

int main()

{
int a,b,i,result;
cout << " Enter a and b: " << endl;
cin >> a >> b;
result = a;                 // At 0 sec result is a
for (i=1; i<=b; i++)
{
result = result*2;      // Doubles the result at every second
cout <<result<< endl;
}
return 0;
}
``````