C Nothing more complicated than functions Ask the user for a
C++ Nothing more complicated than functions
Ask the user for a number between 3 and 100 (inclusive). USING LOOPS, figure out what is the largest power of 2 that is less than or equal to the number entered. For example, if the user entered 6, answer would be 4. If the user entered 100, the answer would be 64. You must do this within a function called powerOfTwo. For example:
Solution
main.cpp
#include <iostream>
using namespace std;
// function declaration
int powerOfTwo(int num);
int main () {
int num;
cout<<\"Enter number between 3 to 100(inclusive) : \";
cin>>num;
if ((num < 3) || (num > 100))
cout << \"Please follow the directions!\" << endl;
else {
int pow;
pow = powerOfTwo(num);
cout << \"The answer is \";
cout << pow;
cout << endl;
}
}
int powerOfTwo(int num) {
int N = num;
int v = 1;
while (v <= N/2)
v *= 2;
return v;
}
Output :
