Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!