Write and test a function called pow_of_two(num) that takes a positive integer num as input and returns another integer that is the largest power of 2 and is less than or equal to num. For example, 1, 2, 4, 8, 16, 32.. are numbers that are the powers of 2, but 3, 5, 6, 7, 9, 10.. are not.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter6: User-defined Functions
Section: Chapter Questions
Problem 17SA: Write the definition of a function that takes as input two decimal numbers and returns first number...
icon
Related questions
Topic Video
Question

please let anyone solve I really need this question to be solved

Problem 2 (Powers of Two Without Going Over)
Write and test a function called pow_of_two(num) that takes a positive integer num as input and
returns another integer that is the largest power of 2 and is less than or equal to num. For example,
1, 2, 4, 8, 16, 32.. are numbers that are the powers of 2, but 3, 5, 6, 7, 9, 10.. are not.
Some sample outputs are here to test.
pow_of_two(1)
pow_of_two(2)
pow_of_two(12)
pow_of_two(20)
pow_of_two(63)
pow_of_two(284) → 256
→ 1
> 2
→ 8
→ 16
> 32
Transcribed Image Text:Problem 2 (Powers of Two Without Going Over) Write and test a function called pow_of_two(num) that takes a positive integer num as input and returns another integer that is the largest power of 2 and is less than or equal to num. For example, 1, 2, 4, 8, 16, 32.. are numbers that are the powers of 2, but 3, 5, 6, 7, 9, 10.. are not. Some sample outputs are here to test. pow_of_two(1) pow_of_two(2) pow_of_two(12) pow_of_two(20) pow_of_two(63) pow_of_two(284) → 256 → 1 > 2 → 8 → 16 > 32
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr