Computer Science, asked by jo00000000r00007, 2 months ago

The "signature" of a function is the first line which gives the return type, the name of the function and the parameters with their types. As an example the signature of the gcd function is

int gcd(int m, int n)

Write a function with the following signature

bool is_power_of_two(int n)

It should return true if the n is a power of 2. Otherwise, it should return false. As an example, for n = 28, the function should return false while for n = 32, it should return true.

Note that n>=1 for all test cases.

Answers

Answered by dy5632877
0

Answer:

goid morning hiiiiiiiiiiiiiii

Answered by dreamrob
0

Program in C++

#include<iostream>

#include<math.h>

using namespace std;

bool is_power_of_two(int n)

{

int p = 0;

int num = 0;

while(num < n)

{

 num = pow(2,p++);

}

if(num == n)

{

 return true;

}

else

{

 return false;

}

}

int main()

{

int n;

cout<<"Enter a number : ";

cin>>n;

if(is_power_of_two(n) == true)

{

 cout<<"Number is a power of 2";

}

else

{

 cout<<"Number is not a power of 2";

}

return 0;

}

Output 1

Enter a number : 28

Number is not a power of 2

Output 2

Enter a number : 32

Number is a power of 2

Similar questions