In: Computer Science
write in C++ as simple as possible please and thanks
don't use header file <bits/stdc++.h>
2
4
8
Don't forget to put comments at the top of your code
Code:
//program which prints numbers powers of 2 in a given
number
#include <iostream>
#include<cmath>
using namespace std;
int main() {
int n;
//prompts the user for input
cout<<"Enter a number less than 10,000: ";
cin>>n;
int arr[20] = { };
cout<<"The array elements are: "<<endl;
//loop to store elements in array
for(int i=1;pow(2,i)<=n;i++)
{
arr[i] = pow(2,i);
cout<<arr[i]<<endl;
}
return 0;
}
Screenshot:-
Output:-