Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Convert c++ Program to Java. Instead of hardcoding numbers in get a input from the user as well to check if it is a power

Convert c++ Program to Java. Instead of hardcoding numbers in get a input from the user as well to check if it is a power of 2. MUST BE RECURSIVE.

1 #include 2 #include 3 #include 4 using namespace std; 5 /* Function to check if x is power of 2 or not */ 6 bool isPowerOf2(int n) 7 { 8 if (n == 0) 9 return false; 10 while (n != 1) 11 { 12 if (n%2 != 0) //Check for remainder if it non zero then not a power of 2 13 return false; 14 return isPowerOf2(n/2); //recursive call to the function 15 } 16 return true; 17 } 18 19 // Driver program 20 int main() 21 { 22 isPowerOf2(128)? printf("Yes "): printf("No "); 23 isPowerOf2(257)? printf("Yes "): printf("No "); 24 isPowerOf2(1023)? printf("Yes "): printf("No "); 25 isPowerOf2(8192)? printf("Yes "): printf("No "); 26 isPowerOf2(65536)? printf("Yes "): printf("No "); 27 return 0; 28 }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions