Question

Given a positive integer, determine if it is a power of two. Output “yes” or “no”....

  1. Given a positive integer, determine if it is a power of two. Output “yes” or “no”. For example, output yes for 32 (2^5) and no for 25.

(Hint: assume it is true with a Boolean variable then use a loop to find a value to show it is false by setting the Boolean variable to false)

C++ CODE PLEASE

Homework Answers

Answer #1
#include<iostream>
#include<cmath>
using namespace std;
int main(){
        int n;
        cout<<"Enter integer: ";
        cin>>n;
        bool flag=0;
        // looping to find powers untii it reaches given value
        for(int i=0;;i++){
            //if it is power of 2 than make flag as true and break
                if(pow(2,i)==n){
                        flag=1;
                        break;
                }
                // if it is greater than n stop the loop
                if(pow(2,i)>n)
                        break;
        }
        if(flag)
                cout<<"True";
        else
                cout<<"False";
}

NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.

Please Like and Support me as it helps me a lot

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using a loop (not the output format character %o), create a program that takes a positive...
Using a loop (not the output format character %o), create a program that takes a positive integer n, and then displays the polynomial for the octal representation of that integer. Use successive division, as demonstrated in the binary conversion example from the lesson, to do this. For example, for n = 157, the program should output the following: 157 = + (5 * 8^0) + (3 * 8^1) + (2 * 8^2) When this part is working properly, “surround” this...
Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that...
Ask user to input any 5-digit positive integer value. Calculate the sum of digits of that number. Assume that the number is positive, integer, 5-digit. Example: 29107 should calculate 2+9+1+0+7 and get 19 Hint: Use the % operator to extract a digit from a number. Use loop(s) Must be in Java
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
design a program that prompts the user to enter a positive integer number and reads the...
design a program that prompts the user to enter a positive integer number and reads the user’s input. If the user input is not a positive number, the program should prompt them repeatedly until they enter a valid input. Once a valid input is received ,the program uses a loop to calculate the sum of the digits of the user’s input and displays the sum. For example, if the user enters the number 94311, the program should print the sum...
Yes x1 it is to the power a and x2 is to the power b. It...
Yes x1 it is to the power a and x2 is to the power b. It is the Cobb Douglas function. Suppose that production function is given by f(x1,x2)=Ax1ax2b. Suppose the costs of the firm are linear and given by C(x1xx2)=(w1x1+w2x2)/B, where B is a parameter denoting the level of cost-minimizing technology of the firm. Assume the firm can charge the price p for their output. (a) Set up the profit function and take the first-order conditions. (b) Suppose that...
Given this algorithm written in pseudocode:   Algo(n) Input: A positive integer n Output: Answer, Algo(n) If...
Given this algorithm written in pseudocode:   Algo(n) Input: A positive integer n Output: Answer, Algo(n) If n = 1 Answer = 3 Else Answer = 3 × Algo(n-1) End if What is Algo(4)? Please provide a detailed answer! thank you!
Be able to write Python programming of a factorial of an integer (given input parameters, output...
Be able to write Python programming of a factorial of an integer (given input parameters, output characteristics and expected output behavior, do not use internal python functions for factorials), recall for example that: 5! = 5 ∗ 4 ∗ 3 ∗ 2 ∗ 1 = 120 and to continue asking unless user says to stop NOT USING FACTORIAL FUNCTION
Calculate the % sensitivity and % specificity of urine pregnancy test given the data below. (Hint:...
Calculate the % sensitivity and % specificity of urine pregnancy test given the data below. (Hint: You will first have to determine if the number in the column is a true positive, true negative, false positive, or false negative before calculating sensitivity and specificity of the test) Table of Number of Pregnancies and Urine Pregnancy test Results Patient is Pregnant? Patient has positive pregnancy test Patient has negative pregnancy test Row totals Yes 32 8 40 No 5 143 148...
write a C program to find whether the given number of three digits is an integer...
write a C program to find whether the given number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is since 3**3 + 7**3 + 1**3 = 371. Output: Enter a number, or -999 to quit : 432 4**3 + 3**3 + 2**3 is not = 432 Enter a number, or -999 to quit : 371 3**3 + 7**3 + 1**3 is = 371...