Sunday, 15 June 2014

c++ - Writing all powers of 2 not exceeding given number -



c++ - Writing all powers of 2 not exceeding given number -

here code.

int n,power=2; cin>>n; cout<<power<<endl; while(power<n){ power=power*2; cout<<power<<endl; }

for example, if wrote 20, output should "2 4 8 16". instead, writes me "2 4 8 16 32" should alter in code? thanks.

you want alter ordering, increment value after checking:

while(power<n){ cout<<power<<endl; power=power*2; }

you want remove first cout remove duplicates.

c++

No comments:

Post a Comment